How to Pass Credentials in PowerShell

In this post we are going to look at the multiple different ways to use user credentials in PowerShell.

Bare in mind, the examples listed in this post aren’t the only options available when it comes to using credentials in PowerShell, but these examples are a good place to start.

Having your domain username and password in a script –

BAD PRACTICE – DEGRADES THE SECURITY OF THE ACCOUNT

$username = “domain\username”
$password = “NotSecurePassword”
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password


Manuel entry for single command use:

$Credentials = Get-Credential

By specifying the ‘Get-Credential’ cmdlet we can enter the user credentials we require

PowerShell Get-Cred


For a script with multiple commands requiring credentials –

You will need to do something similar to the below example as to avoid having to continually input your credentials.

In summary, we are going to enter the required domain password, pass it to the ‘ConvertFrom-SecureString’ cmdlet, which will save the password to a text file in a encrypted string format at the file path C:\test\password.txt

$Credentials = Get-Credential
$Credentials.Password | ConvertFrom-SecureString | Set-Content C:\test\password.txt
$Username = $Credentials.Username
$Password = Get-Content “C:\test\password.txt” | ConvertTo-SecureString
$Credentials = New-Object System.Management.Automation.PSCredential $Username,$Password

Not necessary but if you want to pass your domain password into it’s own variable you can do this:

$Password = $Credentials.GetNetworkCredential().Password


Avoid putting in the domain/username field –

Alternatively if you want to save putting in both your username and your password every time you run the script, you can set it so you will only need to input your password when the script runs.

To do this you would put your domain\username in the script, similar to this –

$username = “domain\username”

and then include the below command at the beginning of the script, which will force you to enter in your domain password upon running the script.

read-host -assecurestring | convertfrom-securestring | out-file C:\test\password.txt

It will look similar to this:

$username = “domain\username”
$password = cat C:\test\password.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

Just like the example above, this will export your domain password to the C drive test folder in an encrypted text document.

Hope this is helpful !

 


Thanks for reading – feel free to follow and stay updated 🙂  View sysadminguides’s profile on Facebook View GuidesSysadmin’s profile on Twitter View 115372466162675927272’s profile on Google+

2 thoughts on “How to Pass Credentials in PowerShell

  1. nice..please remember, proper English is to use the word ‘below’ after the object you’re talking about..
    in other words, pilots wouldn’t say ‘hey , look at the below objects!” they would instead say, ‘hey,look at the objects below’.
    why do people in IT think it’s OK to say things like ‘look at the below table’ instead of ‘look at the table below’?
    when did that become the norm?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s