Get-Password.ps1

# Custom Class to generate random password
# A simple tool to generate random or specified password
Class PasswordGenerator {

    # Static values
    Static [String]$Name = (whoami).Split("\")[1]
    Static [Int]$MaximumValue = 10000

    # Create New password
    [String]NewPassword()
    {
        [Array]$Char = "@",",",".","#","$","&","(",")",";","*","-","_","+","<",">","!","%"
        $Password = [PasswordGenerator]::Name + (Get-Random $Char) + (Get-Random ([PasswordGenerator]::MaximumValue))
        return $Password
    }

    # Generate random password
    [String]RandomPassword([Int]$PasswordLength,[Int]$NoAlphaNumericChar) 
    {
    
        try {
            Add-Type -AssemblyName System.Web
            return $RandomPassword = [System.Web.Security.MemberShip]::GeneratePassword($PasswordLength,$NoAlphaNumericChar)
        }
        catch {
            return Write-Host "$($_.Exception.Message)" -ForegroundColor Red
        }
    }
}

Function Get-Password {
    <#
        .SYNOPSIS
        This script generates random password with the given password length.
 
        .DESCRIPTION
        This script is designed to generate random and strong password for
        the given password length. This helps to adhere to the organisation's
        strong password suggestion/standards by helping the users to create a random and
        strong password.
 
        Additionally this tool comes with a .NET class called PasswordGenerator.
        See examples to know how to use. It is as simple as using the PowerShell
        Cmdlets. Note that the minimum version for using the class in PowerShell
        reaquires v5.0.
 
        .PARAMETER PasswordLength
        Provide the length of your password.
        Example : 10
        NOTE : By default this value is set as 10. This can be override by providing
        the value you need.
 
        .PARAMETER NonAlphaNumericChar
        Provide a number to include non alpha numeric character.
        Example : 2
        NOTE : By default this value is set as 0. This can be override by providing
        the value you need.
 
        .EXAMPLE
        Get-Password
 
        w6tD^oh)tU
 
        .EXAMPLE
        Get-Password -PasswordLength 10 -NonAlphanumericChar 0
 
        (=:y&!$^4O
 
        .EXAMPLE
        Get-Password -PasswordLength 8 -NonAlphanumericChar 2 -Verbose
 
        VERBOSE: [2019/12/22_08:47:44] : Begin function
        VERBOSE: [2019/12/22_08:47:44] : Generating password of length 8
        VERBOSE: [2019/12/22_08:47:44] : Generated Password : +;onIV%%
        VERBOSE: [2019/12/22_08:47:44] : End function
        +;onIV%%
 
        .EXAMPLE
        Using Module PasswordGenerator
 
        $Password = [PasswordGenerator]::new()
 
        $Password.NewPassword()
        Name@1234
 
        $Password.RandomPassword(10,2)
        Ka(o|cVBta
         
        .NOTES
        Author Version Date Notes
        ----------------------------------------------------------------------------
        harish.karthic v1.0 22/12/2019 Initial script
        harish.karthic v1.1 22/12/2019 Converted to advanced function
        harish.karthic v1.2 22/12/2019 Added detailed help and added .NET classes
    #>


    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$false)]
        [Int]$PasswordLength = 10,

        [Parameter(Mandatory=$false)]
        [Int]$NonAlphanumericChar = 0
    )

    begin {
        $funtionName = $MyInvocation.MyCommand.Name

        $Message = "[$(Get-Date -UFormat %Y/%m/%d_%H:%M:%S)] $functionName : Begin function"
        Write-Verbose $Message 
    }

    process {
        $Message = "[$(Get-Date -UFormat %Y/%m/%d_%H:%M:%S)] $functionName : Generating password of length $($PasswordLength)"
        Write-Verbose $Message

        try {
            
            # Loading system assembly
            Add-Type -AssemblyName System.Web
        }
        Catch {
            Write-Host " [$(Get-Date -UFormat %Y/%m/%d_%H:%M:%S)] $functionName : Error at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message).." -ForegroundColor Red
        }

        $Password = [System.Web.Security.Membership]::GeneratePassword($PasswordLength,$NonAlphanumericChar)

        $Message = "[$(Get-Date -UFormat %Y/%m/%d_%H:%M:%S)] $functionName : Generated Password : $($Password)"
        Write-Verbose $Message
        
    }

    end {
        $Message = "[$(Get-Date -UFormat %Y/%m/%d_%H:%M:%S)] $functionName : End function"
        Write-Verbose $Message

        return $Password
    }
}
Export-ModuleMember -Function Get-Password