Gary's $this and that about PowerShell

I am a Microsoft Senior Premier Field Engineer based out of Atlanta, GA. My focus is Powershell. This blog is mainly to share interesting Powershell script samples.

Creating a PS Credential from a Clear Text Password in Powershell

Creating a PS Credential from a Clear Text Password in Powershell

  • Comments 2
  • Likes

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 http://www.microsoft.com/info/cpyright.htm.

Comments
  • nice, perfect and cleanly explained!

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment