How to create a PSCredential object

Leave a comment

October 11, 2012 by Ravi C Khambhati

If you guys have ever worked with powershell you might have came across with this scenario of passing UserID and Password to powershell command. Unlike other command line tool PowerShell is different.

To pass credentials you need to create an object of “PSCredential” and there are two ways to create this object

Method 1: Prompt the user
Below command will prompt user for User ID and Password

$mycredentials = Get-Credential

Method 2: Hardcore the value/read from file
Below code will generate PSCredential object

$securePassword = ConvertTo-SecureString “<Password>” -AsPlainText -Force
$mycredentials= New-Object System.Management.Automation.PSCredential (“<UserName>”, $securePassword)


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: