다음을 통해 공유


Creating a PS Credential from a Clear Text Password in Powershell

Before I begin here, let me say, this is very unsecure.  There are very few times when doing this would be a good idea from a security standpoint.  That being said, it is definately possible.

First things first.  We need to build a secure string for the password:

PS C:\> $password = "mypassword" | ConvertTo-SecureString -asPlainText -Force

We have to use the asPlainText paramater to tell the convertto cmdlet that our source is a plain text string and not an encrypted string which is what it normally expects.  Then, we have to use -force so Powershell knows we really want to do this unsecure technique.

Next we'll put the username into a variable and create the PSCredential object:

PS C:\> $username = "nwtraders\administrator"
PS C:\> $credential = New-Object System.Management.Automation.PSCredential($username,$password)

This creates the new PSCrednetial object that is now ready to be used by any cmdlet that needs a PSCredential like Get-WMIObject:

PS C:\> Get-WMIObject win32_logicaldisk -ComputerName Server1 -Credential $credential

I am not reccomending you do this as it is unsecure, but it works if you really need to.  Remember this applies to anything that is expecting a PSCredential object.  Don't quote me right now, but I am pretty sure that the remote PSSession in Powershell V2 uses a PSCredential (among other cmdlets).

 

 

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.

Comments

  • Anonymous
    August 15, 2013
    nice, perfect and cleanly explained!

  • Anonymous
    July 17, 2014
    Thanks much Gary, 5 years ex post facto, for giving a noob a basic understanding of what a credential is about.

  • Anonymous
    December 23, 2014
    Hi Gray.
    very useful. really thanks

  • Anonymous
    January 21, 2015
    Thanks a lot :-)

  • Anonymous
    October 19, 2015
    Obrigado! Muito bom!

  • Anonymous
    December 07, 2015
    Gary - I know you say you're not recommending this as it's not secure, so I ask that you put a link to using credentials in a secure manner, so people stumbling across this can be led to a better method.

  • Anonymous
    January 15, 2016
    It has been up 7 years, I think the damage is done. I found it useful, cheers +1