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

Comments (7)

  1. CRCerr0r says:

    nice, perfect and cleanly explained!

  2. Randy C. says:

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

  3. john.s2011 says:

    Hi Gray.
    very useful. really thanks

  4. Venky says:

    Thanks a lot 🙂

  5. Raul says:

    Obrigado! Muito bom!

  6. Dave says:

    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.

  7. Darren says:

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