Creating a Credential Object from Plain Text

You can always create a credential object manually, by entering a username and password into the prompt created by the line:


$cred = get-credential

 

While it's not recommended due to a lack of security, however, sometimes you just need to enter a username and password in plain text, to get a script working, or maybe just to test it out. Here's how you can do that. (Just make sure you store your script in a secure location).

Here's the code you need to use:


$domain = "YourDomain.whatever"
$username = "$domain\YourUserAccount"
$password = "YourPassword" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)

 

You can now use this $cred object in any other cmdlet, such as:


$output = Add-Computer -Credential $cred -DomainName $domain

If you look at the contents of the $password variable, you will notice that it simply displays "System.Security.SecureString".
The following command will show you the actual secure string that your password was converted to:


$password | convertfrom-securestring

Which will show you something like:

  
01000000d08c9ddf0115d1118c7a00cd4fc297eb01020000e10d3ef94d301847885e66d6e33749cd000000000200000000001066000000010000200000000f25f3f3f2fd132edf391dfbe928a374fa5e32e8f17b0cfb3a365dd8d3b93bd8000400500e8000000002000020000000d8ca1123fb615474b0f85efe10fdcd5f374249e0b291f65496839c238388a9b820000000f25ac9dcdd8f1df83740301217a3aaa987dd3b2fe928e83f58f3d1312a8021984000000002bb6a8dccf84aa28fc19de8cc0f04f4dd62a4aafd16b2c2f8ce740e34ee90cc48bc1d7071143a1b5b78a84f253ed06f7384c17314e7617073362794636d834d

This string is tied to the user account you're logged in as when you create it. If you are going to run your script under another user - then create the secure password string under THAT account, otherwise it will not work.