PassGen.psm1

function Get-Password {
<#
    .SYNOPSIS
    This commandlet is used to randomly generate passwords.
         
    .DESCRIPTION
    Written by Daniel Caulfield 2016/12/02 This script is provided as-is and without
    any warranty implied or otherwise, use at your own risk.
     
    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
    Set as $True to randomize the order of the password characters
    by default the format is UC Letter, LC Letters, Numbers
     
    .PARAMETER SpecChars
    Adds in xNumber of special character from the following ()~!@&*-+=\{}#$%[]?/
    Supply an integer (default is 0)
 
    .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 $True -SpecChars 3
    c27yt3nkbri9pYo
    k7bfvy4u98ilSad
    ixh7O6mpge08uls
    p8m1oIgewin0v4k
    uof4gy2b5emHlx1
     
    Randomly generates 5 passwords each with a length of 15 characters, with 3 special characters & randomizes it
#>

Param([Int]$Count = 1, [Int]$Length = 8, [Boolean]$Randomize = $false, [Int]$SpecChars = 0)
    if($Length -lt 8){
    Write-Error "Minimum length of 8 characters required"
    exit 1
}
        $Llettercnt = $Length - 5
        $Ulettercnt = 1
        $numcnt = 4
                
  
  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 $SpecChars) -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 $SpecChars) -join ""
            }
            Write-Output $pass
        }
  
  }
    

}

function Get-ComplexPassword {
<#
    .SYNOPSIS
    This commandlet is used to randomly generate passwords.
         
    .DESCRIPTION
    Written by Daniel Caulfield 2016/12/02 This script is provided as-is and without
    any warranty implied or otherwise, use at your own risk.
     
    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
    Set as $True to randomize the order of the password characters
    by default the format is UC Letter, LC Letters, Numbers
     
    .PARAMETER SpecChars
    Adds in xNumber of special character from the following ()~!@&*-+=\{}#$%[]?/
    Supply an integer (default is 5) (special characters in addition to supplied length)
 
    .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 5 special characters, randomized
         
    .EXAMPLE
    Get-ComplexPassword -Count 5 -Length 15 -Randomize $True -SpecChars 3
    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 3 special characters & randomizes it
#>

Param([Int]$Count = 1, [Int]$Length = 15, [Boolean]$Randomize = $True, [Int]$SpecChars = 5)
    if($Length -lt 8){
    Write-Error "Minimum length of 8 characters required"
    exit 1
}
        $Llettercnt = $Length - 8
        $Ulettercnt = 4
        $numcnt = 4
                
  
  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 -gt 0){
                $pass += ($Spec | Get-Random -Count $SpecChars) -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 -gt 0){
                $pass += ($Spec | Get-Random -Count $SpecChars) -join ""
            }
            Write-Output $pass
        }
  
  }
    

}