techgeek.biz being a geek is fun

[ Home ] [ Tech Tips ] [ Getting Started ] [ Contact ] [ 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

Luke Keam

Thank you for reading.

Any questions, comments or suggestions
Email [email protected]

Have a great day from

Luke Keam 
techgeek.biz 
 

AD


                   

        

FOLLOW US

youtubemail

AD