Generate Passwords Using PowerShell

PowerShell Tip: Generate Passwords Using PowerShell

Requirement

Generate Passwords with below formats

  • Password With Lower Case - Minimum 6 and Maximum 8 characters
  • Password With Upper Case - Minimum 6 and Maximum 8 characters
  • Password With Alphanumeric - Minimum 6 and Maximum 8 characters

Summary

As we all know that we can use ASCII Code values from 33 till 126 for passwords. So I am consumption the same in my PowerShell code. There are many ways to achive this. However I am sharing the simple and easiest way.

Code

function Get-MixedPassword
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true, helpmessage = "Minimum 6 and Maximum 8")]
        [ValidateRange(6,8)]
        [int]$length
    )

    -join ($password = @(33..126) | %{[char][int]$_} | Get-Random -Count $length)
    #$password
}
Get-MixedPassword -length 8


Function Get-UpperCasePassword
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true , helpmessage = "Minimum 6 and Maximum 8")]
        [ValidateRange(6,8)]
        [int]$length
    )

    -join ($password = @(66..90) | %{[char][int]$_} | Get-Random -Count $length)

}
Get-UpperCasePassword -length 7

Function Get-LowerCasePassword
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true , helpmessage = "Minimum 6 and Maximum 8")]
        [ValidateRange(6,8)]
        [int]$length
    )

    -join ($password = @(97..122) | %{[char][int]$_} | Get-Random -Count $length)

}
Get-LowerCasePassword -length 6