PassGen.psm1

function Get-Password {
<#
    .SYNOPSIS
    This commandlet is used to randomly generate passwords.
         
    .DESCRIPTION
    This commandlet is used to randomly generate passwords.
     
    Randomly generates passwords of a given length (default/minimum 8 characters)
     
    .FUNCTIONALITY
    Randomly generates passwords of a given length (default/minimum 8 characters)
     
    .PARAMETER Count
    The number of passwords to generate
     
    .PARAMETER Length
    The character length of the passwords to be generated (minimum 8)
     
    .PARAMETER Randomize
    Randomize the order of the password characters
    by default the format is UC Letter, LC Letters, Numbers, [SpecChars]
     
    .PARAMETER SpecChars
    Adds in special character from the following ()~!@&*-+=\{}#$%[]?/
     
 
    .LINK
    http://www.f-e-a-r.co.uk
     
    .EXAMPLE
    Get-Password
    Pstz2470
     
    Randomly generates 1 password at the default length of 8 characters no special characters not randomized (UC Letter, LC Letters, Numbers)
     
    .EXAMPLE
    Get-Password -Count 5 -Length 15 -Randomize
    c27yt3nkbri9pYo
    k7bfvy4u98ilSad
    ixh7O6mpge08uls
    p8m1oIgewin0v4k
    uof4gy2b5emHlx1
     
    Randomly generates 5 passwords each with a length of 15 characters & randomizes it
#>

Param([Int]$Count = 1, [Int]$Length = 8, [Switch]$Randomize, [Switch]$SpecChars)
    if($Length -lt 8){
    Write-Error "Minimum length of 8 characters required"
    exit 1
}
        if ($SpecChars) {
        $Llettercnt = $Length - 7
        $Ulettercnt = 1
        $numcnt = 4
        $SpecCharsCnt = 2
        } else {
        $Llettercnt = $Length - 5
        $Ulettercnt = 1
        $numcnt = 4
        $speccharscnt = 0
        }        
  
  1..$Count | ForEach-Object{
   
        $Num = 48..57  | ForEach-Object {[Char]$_}    #Numbers
        $Lower = 97..122 | ForEach-Object {[Char]$_}  #Lower Case
        $Upper = 65..90  | ForEach-Object {[Char]$_}  #Upper Case
        $Spec = [Char[]]"()~!@&*-+=\{}#$%[]?/"
  
        

        if ($Randomize) {
            $pass = ""
            $pass += ($Upper | Get-Random -Count $Ulettercnt) -join "" 
            $pass += ($Lower | Get-Random -Count $Llettercnt) -join ""
            $pass += ($Num | Get-Random -Count $numcnt) -join ""
            if ($SpecChars){
                $pass += ($Spec | Get-Random -Count $SpecCharsCnt) -join ""
            }
            $pass = ($pass.ToCharArray() | Sort-Object {Get-Random}) -join ""
            Write-Output $pass
        } else{
            $pass = ""
            $pass += ($Upper | Get-Random -Count $Ulettercnt) -join "" 
            $pass += ($Lower | Get-Random -Count $Llettercnt) -join ""
            $pass += ($Num | Get-Random -Count $numcnt) -join ""
            if ($SpecChars){
                $pass += ($Spec | Get-Random -Count $SpecCharscnt) -join ""
            }
            Write-Output $pass
        }
  
  }
    

}

function Get-ComplexPassword {
<#
    .SYNOPSIS
    This commandlet is used to randomly generate passwords.
         
    .DESCRIPTION
    This commandlet is used to randomly generate passwords.
     
    Randomly generates passwords of a given length (default/minimum 8 characters)
     
    .FUNCTIONALITY
    Randomly generates a complex passwords of a given length (minimum 8 characters Default 15 Characters)
     
    .PARAMETER Count
    The number of passwords to generate
     
    .PARAMETER Length
    The character length of the passwords to be generated (minimum 8)
     
    .PARAMETER Randomize
    Randomize the order of the password characters
    by default the format is UC Letter, LC Letters, Numbers
     
    .PARAMETER SpecChars
    Adds in special character from the following ()~!@&*-+=\{}#$%[]?/
     
 
    .LINK
    http://www.f-e-a-r.co.uk
     
    .EXAMPLE
    Get-ComplexPassword
    j}8?b4iet7E]CXa~!f6H
     
    Randomly generates 1 password at the default length of 15 characters with special characters, randomized
         
    .EXAMPLE
    Get-ComplexPassword -Count 5 -Length 15 -Randomize
    0?jM{sc9DL4bXtw]6i
    tI=o&Tjdn10-r7Ev8G
    Jl\h2p4TYn9V7iar?!
    v3KnTWOt4+h-cau8{7
    pULuc]J3Mz5n&)27wt
     
    Randomly generates 5 passwords each with a length of 15 characters, with special characters & randomizes it
#>

Param([Int]$Count = 1, [Int]$Length = 15, [Boolean]$Randomize = $True, [Boolean]$SpecChars = $True)
    if($Length -lt 8){
    Write-Error "Minimum length of 8 characters required"
    exit 1
}

        if ($SpecChars) {
        $Llettercnt = $Length - 8
        $Ulettercnt = 4
        $numcnt = 4
        $SpecCharsCnt = 2
        } else {
        $Llettercnt = $Length - 8
        $Ulettercnt = 4
        $numcnt = 4
        $speccharscnt = 0
        }
                
  
  1..$Count | ForEach-Object{
   
        $Num = 48..57  | ForEach-Object {[Char]$_}    #Numbers
        $Lower = 97..122 | ForEach-Object {[Char]$_}  #Lower Case
        $Upper = 65..90  | ForEach-Object {[Char]$_}  #Upper Case
        $Spec = [Char[]]"()~!@&*-+=\{}#$%[]?/"
  
        

        if ($Randomize -eq $true) {
            $pass = ""
            $pass += ($Upper | Get-Random -Count $Ulettercnt) -join "" 
            $pass += ($Lower | Get-Random -Count $Llettercnt) -join ""
            $pass += ($Num | Get-Random -Count $numcnt) -join ""
            if ($SpecChars -eq $true){
                $pass += ($Spec | Get-Random -Count $SpecCharsCnt) -join ""
            }
            $pass = ($pass.ToCharArray() | Sort-Object {Get-Random}) -join ""
            Write-Output $pass
        } else{
            $pass = ""
            $pass += ($Upper | Get-Random -Count $Ulettercnt) -join "" 
            $pass += ($Lower | Get-Random -Count $Llettercnt) -join ""
            $pass += ($Num | Get-Random -Count $numcnt) -join ""
            if ($SpecChars -eq $true){
                $pass += ($Spec | Get-Random -Count $SpecCharsCnt) -join ""
            }
            Write-Output $pass
        }
  
  }
    

}