Generating Random Passwords

I have created a script that will generate a random password that can be used for setting up accounts or signing up to any web services, etc. The password contains a mixture of letters (lowercase a-z and uppercase A-Z) , numbers and special characters.

You can choose number of characters by typing the minimum value and maximum value and the password will generate between those values.

I have broken down each part of the script to explain how the script works.

function Get-Password
{
    <#
    .Synopsis
       Generates a random characters to use as a password
    .DESCRIPTION
       This function will generate random characters between the mincharacters and maxcharcters numbers you specify. 
    .Parameter MinCharacters
        This Parameter is the smallest amount of characters to use
        This parameter is madatory
    .Parameter MaxCharacters
        This Parameter is the maximum amount of characters to use
        This parameter is madatory
    .EXAMPLE
       To generate a password
       get-password -MinCharacters 6 -MaxCharacters 12
    #>
    [cmdletBinding()]
    param 
    (
        [parameter(Mandatory=$true,
                   HelpMessage="Enter minimum number of characters",
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [INT]$MinCharacters,
        [parameter(Mandatory=$true,
                   HelpMessage="Enter minimum number of characters",
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [INT]$MaxCharacters
    )
    Begin
    {
        $min = $MinCharacters
        $max = $MaxCharacters
    }
    Process
    {
        $characters = Get-Random -Minimum $min -Maximum $max
        Add-Type -AssemblyName System.Web
        $password = [System.Web.Security.Membership]::GeneratePassword($characters, 4)
    }
    End
    {
        $password
    }
}

I created a function called Get-Password to execute the command and I have set up a help page for the script. When you run the script and type get-help Get-Password, you will see the below help information.

function Get-Password
{
    <#
    .Synopsis
       Generates a random characters to use as a password
    .DESCRIPTION
       This function will generate random characters between the mincharacters and maxcharcters numbers you specify. 
    .Parameter MinCharacters
        This Parameter is the smallest amount of characters to use
        This parameter is madatory
    .Parameter MaxCharacters
        This Parameter is the maximum amount of characters to use
        This parameter is madatory
    .EXAMPLE
       To generate a password
       get-password -MinCharacters 6 -MaxCharacters 12
    #>

I then set up the below parameters to control characters range. I called the parameters $MinCharacters and $MaxCharacters. The $minCharacters is the smallest amount of characters you like to use and the $MaxCharacters is the maximum number of characters you like to use for your password.  I also included  [cmdletBinding()] to include default properties such as Write-Verbose, Write-Debug, etc.

 [cmdletBinding()]
    param 
    (
        [parameter(Mandatory=$true,
                   HelpMessage="Enter minimum number of characters",
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [INT]$MinCharacters,
        [parameter(Mandatory=$true,
                   HelpMessage="Enter minimum number of characters",
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [INT]$MaxCharacters
    )

Below the Param function, I used the begin block and added the parameters to a new variable called $min and $max. I like to keep the script organised and therefore used a new variables.

Begin
{
      $min = $MinCharacters
      $max = $MaxCharacters
}

In the process block I have used Get-Random cmdlet to randomnise the characters by using $min in -Minimum parameter and $max in -Maximum parameter. This will randomise the characters with letters and numbers between the values you specify in $MinCharacters and $MaxCharacters variables. I stored Get-Random into a variable called $characters, which will be used later on.

Below the $characters variable, I added a assembly called System.Web to activate the method called GeneratePassword [System.Web.Security.Membership]::GeneratePassword($characters, 4). In this method I have added $characters variable and 4. The value 4 indicates how many non special characters to use in the mix. Non special characters are !ӣ$%^&*(), etc.

I stored the method into a variable called $password. In the End block, I added the $password to output the result.

Process
    {
        $characters = Get-Random -Minimum $min -Maximum $max
        Add-Type -AssemblyName System.Web
        $password = [System.Web.Security.Membership]::GeneratePassword($characters, 4)
    }

The below show an output when executing the command:

Leave a Reply

Your email address will not be published. Required fields are marked *