SOLVED
Home

$cred = Get-Credential without asking for prompts in powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-483274%22%20slang%3D%22en-US%22%3E%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-483274%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20am%20trying%20to%20run%20a%20script%20and%20i%20wanted%20to%20run%20it%20silently%20without%20asking%20for%20credential%20prompts.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EIs%20there%20a%20way%20to%20get%20around%20using%26nbsp%3B%22%24cred%20%3D%20Get-Credential%22%20without%20a%20prompt%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThe%20script%20i%20am%20trying%20to%20use%20is%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%23%20Connect%20to%20Office%20365%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%23%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%24username%20%3D%20%22%22%3CBR%20%2F%3E%24password%20%3D%20ConvertTo-SecureString%20%22mypassword%22%20-AsPlainText%20-Force%3CBR%20%2F%3E%23%24secureStringPwd%20%3D%20%24password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%3CBR%20%2F%3E%24creds%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20(%24user%2C%20%24password)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EImport-Module%20MSOnline%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24cred%20%3D%20Get-Credential%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EConnect-MSolService%20-credential%20%24cred%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fps.outlook.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noreferrer%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fps.outlook.com%2Fpowershell-liveid%2F%3C%2FA%3E%3CSPAN%3E%26nbsp%3B-Credential%20%24cred%20-Authentication%20Basic%20-AllowRedirection%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3EImport-PSSession%20%24Session%20-AllowClobber%20-DisableNameChecking%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-483274%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-483300%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-483300%22%20slang%3D%22en-US%22%3EYou%20can%20store%20passwords%20in%20an%20encrypted%20file%20on%20the%20machine%20you%20are%20running%20the%20script%20on.%20Here%20is%20a%20site%20that%20gives%20some%20examples%20-%20%3CA%20href%3D%22https%3A%2F%2Fblog.kloud.com.au%2F2016%2F04%2F21%2Fusing-saved-credentials-securely-in-powershell-scripts%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblog.kloud.com.au%2F2016%2F04%2F21%2Fusing-saved-credentials-securely-in-powershell-scripts%2F%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20can%20also%20leverage%20windows%20credential%20manager%20to%20store%20credentials%20and%20retrieve%20the%20crowds%20from%20there%20in%20your%20script%20-%20%3CA%20href%3D%22https%3A%2F%2Fbitsofwater.com%2F2018%2F02%2F16%2Fusing-credential-manager-in-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fbitsofwater.com%2F2018%2F02%2F16%2Fusing-credential-manager-in-powershell%2F%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-483570%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-483570%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F327813%22%20target%3D%22_blank%22%3E%40TejCGS%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eusing%20windows%20credential%20manager%2C%20create%20your%20credential%20and%20give%20it%20a%20name%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%2C%20in%20PowerShell%2C%20Wherever%20you%20use%26nbsp%3B%3C%2FP%3E%3CP%3E%24cred%20%3D%20Get-Credential%3C%2FP%3E%3CP%3Ewhich%20prompts%20you%2C%20replace%20that%20with%3C%2FP%3E%3CP%3E%24cred%20%3D%24(Get-StoredCredential%20-Target%20thenameyoustoredyourcredentialunder)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%E2%80%99ll%20need%20to%20install-module%20CredentialManager%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-484640%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-484640%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F327813%22%20target%3D%22_blank%22%3E%40TejCGS%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20corrected%20your%20script%20(removed%20the%20unwanted%20call%20of%20%3CSPAN%3EGet-Credential%3C%2FSPAN%3E%20and%20fixed%20the%20variable%20name%20creds)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24username%20%3D%20%22admin%40domain.com%22%3CBR%20%2F%3E%24password%20%3D%20ConvertTo-SecureString%20%22mypassword%22%20-AsPlainText%20-Force%3CBR%20%2F%3E%24psCred%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20(%24username%2C%20%24password)%3CBR%20%2F%3EImport-Module%20MSOnline%3CBR%20%2F%3EConnect-MSolService%20-Credential%20%24psCred%3CBR%20%2F%3E%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20https%3A%2F%2Fps.outlook.com%2Fpowershell-liveid%2F%20-Credential%20%24psCred%20-Authentication%20Basic%20-AllowRedirection%20%3CBR%20%2F%3EImport-PSSession%20%24Session%20-AllowClobber%20-DisableNameChecking%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-487635%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-487635%22%20slang%3D%22en-US%22%3Ei%20stored%20my%20credentials%20in%20credentials%20manager%20as%20CGSPS1%20and%20i%20tried%20retrieving%20them%20in%20ps1%3CBR%20%2F%3E%24psCred%20%3D%20Get-StoredCredential%20-Target%20%22CGSPS1%22%20%E2%80%93AsCredentialObject%3CBR%20%2F%3E%3CBR%20%2F%3E%24CRED%20%3D%20Get-StoredCredential%20-Target%20%22CGSPS1%22%20%E2%80%93AsCredentialObject%3CBR%20%2F%3EConnect-MSolService%20-Credential%20%24CRED%3CBR%20%2F%3Eand%20i%20get%20the%20below%20error%3CBR%20%2F%3E%3CBR%20%2F%3EConnect-MsolService%20%3A%20Cannot%20bind%20parameter%20'Credential'.%20Cannot%20convert%20the%20%22PSCredentialManager.Common.Credential%22%20value%20of%20t%3CBR%20%2F%3E%22PSCredentialManager.Common.Credential%22%20to%20type%20%22System.Management.Automation.PSCredential%22.%3CBR%20%2F%3EAt%20line%3A1%20char%3A33%3CBR%20%2F%3E%2B%20Connect-MSolService%20-Credential%20%24cred%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-487637%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-487637%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F519%22%20target%3D%22_blank%22%3E%40Ben%20Stegink%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ei%20stored%20my%20credentials%20in%20credentials%20manager%20as%20CGSPS1%20and%20i%20tried%20retrieving%20them%20in%20ps1%3CBR%20%2F%3E%24psCred%20%3D%20Get-StoredCredential%20-Target%20%22CGSPS1%22%20%E2%80%93AsCredentialObject%3C%2FP%3E%3CP%3E%24CRED%20%3D%20Get-StoredCredential%20-Target%20%22CGSPS1%22%20%E2%80%93AsCredentialObject%3CBR%20%2F%3EConnect-MSolService%20-Credential%20%24CRED%3CBR%20%2F%3Eand%20i%20get%20the%20below%20error%3C%2FP%3E%3CP%3EConnect-MsolService%20%3A%20Cannot%20bind%20parameter%20'Credential'.%20Cannot%20convert%20the%20%22PSCredentialManager.Common.Credential%22%20value%20of%20t%22PSCredentialManager.Common.Credential%22%20to%20type%20%22System.Management.Automation.PSCredential%22.At%20line%3A1%20char%3A33%2B%20Connect-MSolService%20-Credential%20%24cred%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-487684%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-487684%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F327813%22%20target%3D%22_blank%22%3E%40TejCGS%3C%2FA%3E%26nbsp%3BTry%20it%20without%20the%26nbsp%3B%3CSPAN%3E%E2%80%93AsCredentialObject%26nbsp%3B%20%3F%20Does%20that%20make%20any%20difference%3F%20Also%2C%20I%20surrounded%20the%20expression%20with%20%24()%20to%20force%20PowerShell%20to%20interpret%20it.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-488711%22%20slang%3D%22en-US%22%3ERe%3A%20%24cred%20%3D%20Get-Credential%20without%20asking%20for%20prompts%20in%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-488711%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F327813%22%20target%3D%22_blank%22%3E%40TejCGS%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20corrected%20your%20script%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24psCred%20%3D%20Get-StoredCredential%20-Target%20%22CGSPS1%22%3CBR%20%2F%3EConnect-MSolService%20-Credential%20%24psCred%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20664px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110708i8EA622A9CEE23924%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22get-stored-credential.png%22%20title%3D%22get-stored-credential.png%22%20%2F%3E%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
TejCGS
New Contributor

I am trying to run a script and i wanted to run it silently without asking for credential prompts.

Is there a way to get around using "$cred = Get-Credential" without a prompt 

 

The script i am trying to use is 

# Connect to Office 365 

$username = ""
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
#$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($user, $password)

Import-Module MSOnline 
$cred = Get-Credential 
Connect-MSolService -credential $cred 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection 
Import-PSSession $Session -AllowClobber -DisableNameChecking 

 

 

6 Replies
You can store passwords in an encrypted file on the machine you are running the script on. Here is a site that gives some examples - https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/

You can also leverage windows credential manager to store credentials and retrieve the crowds from there in your script - https://bitsofwater.com/2018/02/16/using-credential-manager-in-powershell/

@TejCGS 

using windows credential manager, create your credential and give it a name

 

Then, in PowerShell, Wherever you use 

$cred = Get-Credential

which prompts you, replace that with

$cred =$(Get-StoredCredential -Target thenameyoustoredyourcredentialunder)

 

You’ll need to install-module CredentialManager

Solution

@TejCGS 

 

I have corrected your script (removed the unwanted call of Get-Credential and fixed the variable name creds)

 

$username = "admin@domain.com"
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Import-Module MSOnline
Connect-MSolService -Credential $psCred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $psCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking

 

i stored my credentials in credentials manager as CGSPS1 and i tried retrieving them in ps1
$psCred = Get-StoredCredential -Target "CGSPS1" –AsCredentialObject

$CRED = Get-StoredCredential -Target "CGSPS1" –AsCredentialObject
Connect-MSolService -Credential $CRED
and i get the below error

Connect-MsolService : Cannot bind parameter 'Credential'. Cannot convert the "PSCredentialManager.Common.Credential" value of t
"PSCredentialManager.Common.Credential" to type "System.Management.Automation.PSCredential".
At line:1 char:33
+ Connect-MSolService -Credential $cred

@TejCGS Try it without the –AsCredentialObject  ? Does that make any difference? Also, I surrounded the expression with $() to force PowerShell to interpret it.

@TejCGS 

 

I have corrected your script :

 

$psCred = Get-StoredCredential -Target "CGSPS1"
Connect-MSolService -Credential $psCred

 

get-stored-credential.png 

Related Conversations