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 (11)

  1. Many thanks – just what I was looking for!

  2. Scott Alvarino From Techjunkie.tv says:

    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.tv

  3. kim says:

    So the “PlainTextPassword” is still in plain text in a file somewhere?

    So not secure at all really?

  4. Jake2010 says:

    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.

  5. DavidLean says:

    @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"

  6. Neha says:

    I have networkCredential object. Can i convert it to PSCredential object in  order to connect to PowerShell.?

  7. 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.  

  8. Clinton says:

    Thank you for the clear and concise snippet!

  9. patrick says:

    Is there a way to store that $mycreds object for later use?

  10. PS Noob says:

    Thank you for sharing! This article is very useful to me

  11. Ravindra Somvanshi says:

    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)

    =======================================================

Skip to main content