Created
June 7, 2023 08:27
-
-
Save Digiover/d74a76efedf1e84ddaf947b7284dfe2a to your computer and use it in GitHub Desktop.
Easily create a random string (or secure password) using PowerShell. Add to your PS profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to your PS profile to create random strings / secure passwords | |
# from within your PowerShell shell. | |
# | |
# Source / Author: Daniel Kåven | |
# https://teams.se/powershell-script-generate-a-random-password/ | |
function Get-RandomString { | |
param ( | |
[CmdletBinding(PositionalBinding=$false)] | |
[Parameter(Position=0)] | |
[ValidateRange(8, 256)] | |
[int] $Length = 20, | |
[Parameter(Position=1)] | |
[validateset("AlphaNumeric", "SQLCompliant")] | |
[string]$Compliancy | |
) | |
$Characters = [char]65..[char]90 # A..Z | |
$Characters += [char]97..[char]122 # a..z | |
$Characters += [char]48..[char]57 # 0..9 | |
Switch ($Compliancy){ | |
"AlphaNumeric" { | |
} | |
"SQLCompliant" { | |
$Characters += [char]33 #! | |
$Characters += [char]35..[char]37 # #$% | |
} | |
default { | |
$Characters += [char]33..[char]47 # !"#&%'()*+,-./ | |
} | |
} | |
$Password = @() | |
For ($i = 0; $i -lt $Length; $i++) { | |
$Password += $Characters | Get-Random | |
} | |
return -join $Password | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples (when added to
$PROFILE
):