PowerShell – Encoding a Password

email me

Simple Encoding

Clear-Host
write-host ""

[string]$Password = "LetMeIn99$"
 
$EncodedPassword = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Password))
 
write-host "Encoded Password: " $EncodedPassword

write-host ""

 

Simple Decoding

$DecodedPassword = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($EncodedPassword))
 
write-host "Decoded Password: " $DecodedPassword
write-host ""
write-host ""

 

More Advanced Example

Clear-Host

# PART1
# THIS WORKS TO CREATE UNIQUENESS TO THE SECURESTRING
# IT IS CRITICAL THE KEY IS STORED IN A SECURE LOCATION OR
# COMPILED IN A MANNER THAT IS SECURE
# https://docs.microsoft.com/en-us/azure/storage/common/customer-managed-keys-configure-key-vault
[Byte[]] $Key = (2, 1, 5, 3, 2, 8, 4, 1, 7, 16, 9, 2, 13, 3, 12, 15)

$EncryptedPassword = "LetMeIntoTheComputer777$" | ConvertTo-SecureString -AsPlainText -Force

# PART2
$Password = "76492d1116743f0423413b16050a5345MgB8ADQAVgBzADQAdABCAEYAKwBlAHEAcgBqAE4AMQB2AGoAYQB0AHYAWgBOAGcAPQA9AHwAYwBiAGMAOABkADAAMQAxAGUAMgA5ADkAZABmAGQANAA4AGEAMgA4AGUAYwA2ADYAOQA3ADgAMwA0AD
gAMQAxADgAZgBhADEAOAA2ADIANgAwADUAOABiAGYAOQBiADEANwAyADMAYgAxAGYAYQA4ADkAYwAyADgAOAA5ADEAYgA5ADUAYgBmAGQAYQAxAGYAOQAxAGEANABhAGYAMwAyADYAZgA4ADQAYgA3AGQAYQAyAGMAZQBiADIAYQBmAGYANgA5
ADkAMgBkAGEAZAAzADAAZgA1AGQAYQAxADQANwBlAGEAZgA3ADEAZABjADYAOQA3ADEANQAyADQAMgAyAA=="

$SecurePassword = ConvertTo-SecureString $Password -key $Key

$SecurePassword | ConvertFrom-SecureString -key $Key

$UserAccount = Get-LocalUser -Name "Administrator"

$UserAccount | Set-LocalUser -Password $SecurePassword