DictionaryFile.psm1

function Get-PasswordNumber {
<#
.SYNOPSIS
 
Calculates the number of possible passwords
 
.DESCRIPTION
 
Calculates the number of possible passwords based
on the input so you will know the actual number before
you proceed to create your dictionary file.
 
.PARAMETER Characters
 
Specifies the letters that need to be included.
Parameter is mandatory.
 
.PARAMETER Numbers
 
Specifies the numbers that need to be included.
Parameter is mandatory.
 
.PARAMETER Symbols
 
Specifies the Symbols that need to be included.
Parameter is mandatory.
 
.PARAMETER MinCharacters
 
Specifies the minimum characters of the generated passwords.
Parameter is mandatory.
 
.PARAMETER MaxCharacters
 
Specifies the maximum characters of the generated passwords.
Parameter is mandatory.
 
.PARAMETER IncludeCapital
 
Specifies whether or not to include upper case letters along with
the lower case letters.
 
.PARAMETER CapitalOnly
 
Specifies whether or not all lower case letters to be converted to
upper case letters.
 
.INPUTS
 
None. You cannot pipe objects to Get-PasswordNumber.
 
.OUTPUTS
 
System.String. Get-PasswordNumber returns the number of password that
can be created.
 
.EXAMPLE
 
C:\PS> Get-PasswordNumber -Characters "a,b,c" -Numbers "1,2,3" -Symbols "$,*,&" -MinCharacters 2 -MaxCharacters 5
There are 66420 possible passwords
 
.EXAMPLE
 
C:\PS> Get-PasswordNumber -Characters "a,b,c" -Numbers "1,2,3" -Symbols "$,*,&" -MinCharacters 2 -MaxCharacters 5 -IncludeCapital
There are 271440 possible passwords
 
.EXAMPLE
 
C:\PS> Get-PasswordNumber -Characters "a,b,c" -Numbers "1,2,3" -Symbols "$,*,&" -MinCharacters 2 -MaxCharacters 5 -CapitalOnly
There are 66420 possible passwords
 
.EXAMPLE
 
C:\PS> Get-PasswordNumber -Characters alphabet -Numbers "1,2,3" -Symbols "$,*,&" -MinCharacters 2 -MaxCharacters 5
There are 34636800 possible passwords
 
.LINK
 
https://www.sconstantinou.com/powershell-module-dictionaryfile
#>


    [cmdletbinding()]

    param(
        [parameter(Mandatory = $true)][String[]]$Characters,
        [parameter(Mandatory = $true)][String[]]$Numbers,
        [parameter(Mandatory = $true)][String[]]$Symbols,
        [parameter(Mandatory = $true)][Uint32]$MinCharacters,
        [parameter(Mandatory = $true)][Uint32]$MaxCharacters,
        [switch]$IncludeCapital,
        [switch]$CapitalOnly)

    if ($MinCharacters -eq "0"){
        throw "MinCharacters value cannot zero."
    }

    if ($Characters -eq "alphabet"){
        $Characters = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
    }

    if ($CapitalOnly -eq $True){
        $Characters = $Characters.ToUpper()
    }

    if ($IncludeCapital -eq $True){
        $CharactersCapital = $Characters.ToUpper()
        $Characters = $Characters + $CharactersCapital
    }

    $AllCharacters = $Characters + $Numbers + $Symbols
    $pool = $AllCharacters -split ','
    $pool = $pool | Select-Object -Unique
    [Uint32]$n = $pool.count

    switch ($MinCharacters){
        {$_ -eq $MaxCharacters}{
            [Uint32]$n = $pool.count
            [Uint32]$r = $_
            [Double]$TotalPossibilities = [math]::Pow($n,$r)
            Write-Output "There are $TotalPossibilities possible passwords"
        }
        {$_ -gt $MaxCharacters}{
            throw "MinCharacters value cannot be greater than MaxCharacters value"
        }
        {$_ -lt $MaxCharacters}{
            [int]$TotalPossibilities = 0

            for ($i = $MinCharacters;$i -le $MaxCharacters; $i++){
                [Uint32]$n = $pool.count
                [Uint32]$r = $i
                [Double]$TempPossibilities = [math]::Pow($n,$r)
                [Double]$TotalPossibilities = $TotalPossibilities + $TempPossibilities
            }

            Write-Output "There are $TotalPossibilities possible passwords"
        }
    }
}
New-Alias -Name gpn -Value Get-PasswordNumber

Function Get-Password {

    Param(
        [Object[]]$FullSet,
        [String]$Comma,
        [Uint32]$CurrentIndex = 0,
        [String]$TemporaryText = "",
        [String]$OutFile)

    $MaximumIndex = $FullSet.Count - 1
    $FullSet[$CurrentIndex] |
        ForEach-Object {
            [Array]$Password = "$($TemporaryText)$($Comma)$($_)".Trim($Comma)

            If ($CurrentIndex -lt $MaximumIndex) {
                $Password = Get-Password $FullSet -CurrentIndex ($CurrentIndex + 1) -TemporaryText $Password -OutFile $OutFile
            }

            Add-Content -Path $OutFile -Value $Password
        }
}

function Get-DictionaryFile {
<#
.SYNOPSIS
 
Calculates, creates and saves the possbile passwords
 
.DESCRIPTION
 
Calculates the number of possible passwords based
on the input. After the calculations has been completed
it will need your confirmation to proceed with the creation
of the possible passwords. If you access it will create the
passwords and save them in a dictionary file.
 
.PARAMETER Characters
 
Specifies the letters that need to be included.
Parameter is mandatory.
 
.PARAMETER Numbers
 
Specifies the numbers that need to be included.
Parameter is mandatory.
 
.PARAMETER Symbols
 
Specifies the Symbols that need to be included.
Parameter is mandatory.
 
.PARAMETER MinCharacters
 
Specifies the minimum characters of the generated passwords.
Parameter is mandatory.
 
.PARAMETER MaxCharacters
 
Specifies the maximum characters of the generated passwords.
Parameter is mandatory.
 
.PARAMETER IncludeCapital
 
Specifies whether or not to include upper case letters along with
the lower case letters.
 
.PARAMETER CapitalOnly
 
Specifies whether or not all lower case letters to be converted to
upper case letters.
 
.PARAMETER Path
 
Specifies the path and file that generated passwords will be saved.
(Default: $HOME\DictionaryFiles\DictionaryFile.txt)
 
.INPUTS
 
None. You cannot pipe objects to Get-PasswordNumber.
 
.OUTPUTS
 
System.String. Get-PasswordNumber returns the generated password to
the specified file.
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5
There are 3900 possible passwords
Are you sure you want to create the dictionary file (y or n): y
WARNING: Folder 'C:\Users\stephanos.constantin\DictionaryFiles' does not exist.
Creating Folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
Folder 'C:\Users\stephanos.constantin\DictionaryFiles' created.
File 'DictionaryFile.txt' does not exist in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Creating File 'DictionaryFile.txt' in folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
File 'DictionaryFile.txt' created in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Generating passwords...
Dictionary File creation COMPLETED!!!
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5
There are 3900 possible passwords
Are you sure you want to create the dictionary file (y or n): n
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5 -Path C:\Scripts_Output\MyFile.txt
There are 3900 possible passwords
Are you sure you want to create the dictionary file (y or n): y
File 'MyFile.txt' does not exist in folder 'C:\Scripts_Output'.
Creating File 'MyFile.txt' in folder 'C:\Scripts_Output'...
File 'MyFile.txt' created in folder 'C:\Scripts_Output'.
Generating passwords...
Dictionary File creation COMPLETED!!!
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5 -IncludeCapital
There are 37440 possible passwords
Are you sure you want to create the dictionary file (y or n): y
WARNING: File 'DictionaryFile.txt' already exists in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
WARNING: Removing File 'DictionaryFile.txt' from folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
File 'DictionaryFile.txt' removed from folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Creating new DictionaryFile.txt file in folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
New 'DictionaryFile.txt' file created in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Generating passwords...
Dictionary File creation COMPLETED!!!
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5 -CapitalOnly
There are 3900 possible passwords
Are you sure you want to create the dictionary file (y or n): y
WARNING: File 'DictionaryFile.txt' already exists in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
WARNING: Removing File 'DictionaryFile.txt' from folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
File 'DictionaryFile.txt' removed from folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Creating new DictionaryFile.txt file in folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
New 'DictionaryFile.txt' file created in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Generating passwords...
Dictionary File creation COMPLETED!!!
 
.EXAMPLE
 
C:\PS> Get-DictionaryFile -Characters "a,b,c" -Numbers "1" -Symbols "$" -MinCharacters 2 -MaxCharacters 5
There are 34636800 possible passwords
Are you sure you want to create the dictionary file (y or n): y
WARNING: Folder 'C:\Users\stephanos.constantin\DictionaryFiles' does not exist.
Creating Folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
Folder 'C:\Users\stephanos.constantin\DictionaryFiles' created.
File 'DictionaryFile.txt' does not exist in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Creating File 'DictionaryFile.txt' in folder 'C:\Users\stephanos.constantin\DictionaryFiles'...
File 'DictionaryFile.txt' created in folder 'C:\Users\stephanos.constantin\DictionaryFiles'.
Generating passwords...
Dictionary File creation COMPLETED!!!
 
.LINK
 
https://www.sconstantinou.com/powershell-module-dictionaryfile
#>


    [cmdletbinding()]

    param(
        [parameter(Mandatory = $true)][String[]]$Characters,
        [parameter(Mandatory = $true)][String[]]$Numbers,
        [parameter(Mandatory = $true)][String[]]$Symbols,
        [parameter(Mandatory = $true)][Uint32]$MinCharacters,
        [parameter(Mandatory = $true)][Uint32]$MaxCharacters,
        [Switch]$IncludeCapital,
        [Switch]$CapitalOnly,
        [String]$Path = "$HOME\DictionaryFiles\DictionaryFile.txt")

    $PasswordSpecs = @{
        Characters = $Characters
        Numbers = $Numbers
        Symbols = $Symbols
        MinCharacters = $MinCharacters
        MaxCharacters = $MaxCharacters
        IncludeCapital = $IncludeCapital
        CapitalOnly = $CapitalOnly}

    Get-PasswordNumber @PasswordSpecs

    do{$UserConfirmation = Read-Host -Prompt 'Are you sure you want to create the dictionary file (y or n)'
        If (($UserConfirmation -ieq "n") -or ($UserConfirmation -ieq "y")){
            $UserInput = "Correct"
        }
        else{
            $UserInput = "Wrong"
            $Warning = "You have entered a wrong answer.Please enter y [YES] or n [NO]"
            Write-Warning $Warning
        }
    }
    while ($UserInput -eq "Wrong")

    if ($UserConfirmation -ieq "n"){Break}

    $Directory = Split-Path $Path
    $File = [System.IO.Path]::GetFileName($Path)

    if ($Characters -eq "alphabet"){
        $Characters = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
    }

    if ($CapitalOnly -eq $True){
        $Characters = $Characters.ToUpper()
    }

    if ($IncludeCapital -eq $True){
        $CharactersCapital = $Characters.ToUpper()
        $Characters = $Characters + $CharactersCapital
    }

    if (-not (Test-Path -Path $Directory -PathType Container)){
        Write-Warning "Folder '$Directory' does not exist."
        Write-Output "Creating Folder '$Directory'..."
        New-Item -Path $Directory -ItemType Directory -Force |
            Out-Null
        Write-Output "Folder '$Directory' created."
    }

    if (-not (Test-Path -Path $Path -PathType Leaf)){
        Write-Output "File '$File' does not exist in folder '$Directory'."
        Write-Output "Creating File '$File' in folder '$Directory'..."
        New-Item -Path $Directory -Name $File -ItemType File -Force |
            Out-Null
        Write-Output "File '$File' created in folder '$Directory'."
    }
    else{
        Write-Warning "File '$File' already exists in folder '$Directory'."
        Write-Warning "Removing File '$File' from folder '$Directory'..."
        Remove-Item -Path "$Path" -Force | Out-Null
        Write-Output "File '$File' removed from folder '$Directory'."
        Write-Output "Creating new $File file in folder '$Directory'..."
        New-Item -Path $Directory -Name $File -ItemType File -Force |
            Out-Null
        Write-Output "New '$File' file created in folder '$Directory'."
    }

    $AllCharacters = $Characters + $Numbers + $Symbols
    $pool = $AllCharacters -split ','
    [Array]$pool = $pool |
        Select-Object -Unique

    Write-Output "Generating passwords..."

    for ($i = $MinCharacters; $i -le $MaxCharacters; $i++){
        $Passwords = @()

        for ($p = 1;$p -le $i;$p++){
            [Object[]]$Passwords += ,$pool
        }

        Get-Password $Passwords -OutFile "$Path"
    }

    Write-Output "Dictionary File creation COMPLETED!!!"
}
New-Alias -Name gdf -Value Get-DictionaryFile