PowerShell - How to create a PSCredential object
Several PowerShell commandlets take a PSCredential object to run using a particular user account. You can create the PSCredential object by using Get-Credential commandlet which opens a dialog to enter the username and password. This way of entering credentials can be used in an interactive mode.
$mycredentials = Get-Credential
When you have to provide credentials in non-interactive mode, you can create a PSCredential object in the following way.
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
You can now pass $mycreds to any -PSCredential input parameter
* You may not want to reveal PlainTextPassword for your personal accounts as a general precaution. Use a common user account and password that everyone knows for your non-interactive PSCredential usage.
* Tested with PowerShell 2.0
Comments
Anonymous
August 27, 2010
Many thanks - just what I was looking for!Anonymous
December 27, 2010
Great post it works great now i can use the send-mailmessage cmdlet in Powershell 2.0. Here is the link to my site so you can check it out mashed up with your code techjunkie.tv/how-to-use-send-mailmessage-with-a-password-in-powershell-script Thanks for the code. Very helpful for my script Scott Alvarino From Techjunkie.tvAnonymous
May 05, 2011
So the “PlainTextPassword” is still in plain text in a file somewhere? So not secure at all really?Anonymous
October 26, 2011
Thanks for this article. I was struggling for a while with the PSCredential object as it was giving me an incorrect password error. Finally I used the Get-Credential and that worked great. Which made me dig back and see why the creating of PSCredential via non-interactive did not work. Finally realized that I had a $ sign in my password which may have been confusing powershell to think it was a variable. I am sure there is a way to escape the $ sign but I'll deal with it another day.Anonymous
September 15, 2012
@jacobUT The way to "escape the $ sign" is to use the grave-accent() . On English keyboards it is the button to the left of the (1) key & above the (Tab) key. Typically is also has a tilda (~) above it. The (
) character is escape character for all powershell. It says "Ignore the special meanding of the next character & treat it like a normal character. Similar function to the "" characger in C++ & C# code. eg: to put quotes within a string your use $MyQuote = "He said"Friends, Romans, countrymen, lend me you rears
" to the audience"Anonymous
September 03, 2013
I have networkCredential object. Can i convert it to PSCredential object in order to connect to PowerShell.?Anonymous
December 03, 2013
Just as an additional comment you can add credentials as stored encrypted strings. Still not perfect but a good addition that I use. [System.Management.Automation.PSCredential]$Credential = (new-object -TypeName System.Management.Automation.PSCredential -ArgumentList ('SomeAccount', (ConvertTo-SecureString -String (Import-Clixml C:SecString.xml) -Key (Import-Clixml C:SomePreSharedKey.xml)))) Of course that's assuming you encrypted a string in advance using a key that you recorded in a CliXML export.Anonymous
June 22, 2014
Thank you for the clear and concise snippet!Anonymous
August 06, 2014
Is there a way to store that $mycreds object for later use?Anonymous
April 28, 2015
Thank you for sharing! This article is very useful to meAnonymous
September 04, 2015
Helping article. "Get-Credential" won't work if you compile your ps1 to exe. Below Code, did the trick for me. ====================================================== $user = Read-Host "Enter UserName" $passwd = Read-Host "Enter Password" -AsSecureString $passwd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)) $secpasswd = ConvertTo-SecureString "$passwd" -AsPlainText -Force $creden = New-Object System.Management.Automation.PSCredential ("$user", $secpasswd) =======================================================