Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active January 10, 2020 04:48
Show Gist options
  • Save wsmelton/cb5dd721787405fe58b7ba039843080d to your computer and use it in GitHub Desktop.
Save wsmelton/cb5dd721787405fe58b7ba039843080d to your computer and use it in GitHub Desktop.
Create a snippet for PSCredential in VS Code

Sometimes you just need to create a PSCredential object in your PowerShell script. In VS Code I was not able to find any snippet that did this so created one.

You can add this to the powershell.json snippet file for your VS Code instance. Use Command Palette > Configure User Snippets > PowerShell to open that file.

Then paste the code in the json file of this Gist.

You can use either cred or pscred to bring up the snippet in any editor set to the PowerShell language:

"pscredential": {
"prefix": ["cred","pscredential"],
"body": [
"\\$pwd = \"${1}\" | ConvertTo-SecureString -AsPlainText -Force",
"\\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ${2:username},\\$pwd"
],
"description": "Create a PSCredential object to be used in your script, by @wsmelton"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment