How to create a PSCredential object

25 March,2020 by Tom Collins

Creating a Credential Object  is useful when you require to pass a username and password through Powershell commands.

You might see Credential used in various Powershell cmdlets - such as SqlVulnerabilityAssessmentScan. An example of invoking the SqlVulnerabilityAssessmentScan may be something like:

Invoke-SqlVulnerabilityAssessmentScan -Credential "sa" -ServerInstance myserver\inst1 -DatabaseName "master"

The problem with the way Credential is used above - will create a request for the password. What might be more efficient is to create a secure object with username \ password

Enter the PSCredential. A PSCredential object represents a set of security credentials, i.e username and password.

Here is an example of applying creating a PSCredential object and applying to when using the Powershell cmdlet .

In this example - i've hardcoded the password but in a real scenario you'd store this in some sort of encrypted vault. 

[System.Security.SecureString]$SecPwd = ConvertTo-SecureString -String $pw -AsPlainText -Force
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist @($un,$SecPwd)

Invoke-SqlVulnerabilityAssessmentScan -Credential $Cred -ServerInstance myserver\inst1 -DatabaseName "master"

Expand your Powershell mind – Three key cmdlets

Author: Tom Collins (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on How to create a PSCredential object | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer