[ Home ] [ Tech Tips ] [ Raspberry Pi ] [ Powershell ] [ Ubuntu ] [ Django ] [ About ]
PowerShell | Storing Secure Passwords with PSCredential

PowerShell | Storing Secure Passwords with PSCredential 

 

We want secure Passwords to use in our scripts rather than leaving them in plain text. So this is how we do it. 

FIRE UP THE ISE!!!

  1. Setup 

We run this in the working directory of your script to setup the “SecurePassword.txt” File. It will prompt you for your password.

Read-Host “Input Password” -AsSecureString | ConvertFrom-SecureString | Out-File .\SecurePassword.txt

To put your mind at ease you can run

 

Get-Content .\SecurePassword.Txt

to see how your password is being stored.

 

2. Put into your Script 

Edit the Username for your account.  And TA DA! Done like a turkey.

$UserName = “[email protected]
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password

As long as you keep the text file in the same directory

 

This is the template i use for making credential files. I hope it helps

<#
.SYNOPSIS
Setting up and using a credential template

.DESCRIPTION

.EXAMPLE

.NOTES
Author. Luke Keam
Published. on 20/11/2016 AUS
Version. 1
Company Name. techgeek.biz
Copyright. Free for personal and commercial use. As long as reference kept.

Questions, comments, suggestions always welcome. Please email me.

.LINK
Website. /techtips/powershell-storing-secure-passwords-with-pscredential/
Website. techgeek.biz
Email. [email protected]
#>

# Set a password by running this
Read-Host “Input Password” -AsSecureString | ConvertFrom-SecureString | Out-File .\SecurePassword.txt
# Then leave this template in script that you are using
$UserName = “[email protected]
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password
Send-MailMessage -Credential $Credential

 

Hope this was helpful

 

Have fun  


Luke Keam
Thank you for reading. Any questions, comments or suggestions email me [email protected]
Luke Keam
techgeek.biz

FOLLOW US

Name
Email:

AD

quick-time-management logo.biz

Quick-Time-Management.com

We strive to make you healthy and productive for the long haul. Easily look back and see how you effective you have been.