PowerShell: New-Password Function

Scriptimus PowerShell Banner

OK, so I’ve been off the grid for a while now. But that doesn’t mean I’ve not been busy. Quite the opposite in fact. I’ve taken a year out from work on paternity with my baby Son. During this time, I did a lot of training in my lab developing new skills and solidifying other more fundamental skills. I’ve also passed a couple of certified exams and have built a new lab from a new HP Micro-server that I juiced up. I’m currently planning to take the MCSA: Server 2012 R2 exam and have been training in my lab and doing a lot of PowerShell DSC based automation. Hopefully I’ll be able to share some if not all of this knowledge and experience soon. I thought it best to return with a simple post so here goes.

During my time off I wrote a simple function to generate a password from random characters. I posted it yesterday here in the Microsoft Script Center Script Repository . Here’s a quick screenshot demo:


And here’s a simplified version for discussion.

Function New-Password {








        If ($Uppercase) {$CharPool += ([char[]](65..90))}
        If ($LowerCase) {$CharPool += ([char[]](97..122))}
        If ($Numeric) {$CharPool += ([char[]](48..57))}
        If ($Symbolic) {$CharPool += ([char[]](33..47))
                       $CharPool += ([char[]](33..47))}

        If ($CharPool -eq $null) {
            Throw 'You must select at least one of the parameters "Uppercase" "LowerCase" "Numeric" or "Symbolic"'

        (Get-Random -InputObject $CharPool -Count $length) -join ''


The only issue with this was working out each of the character types. Fortunately I learned, through testing, that you could return a series of ASCII characters by indexing them.


PowerShell Characters

For example, this line of code places the lowercase characters a-z in a variable (really a string type object) called $L.

$L = ([char[]](97..122))

PowerShell Characters LowerCase

Here you add 92 characters in turn to a string object called $CharPool.

$CharPool += ([char[]](65..90))
$CharPool += ([char[]](97..122))
$CharPool += ([char[]](48..57))
$CharPool += ([char[]](33..47))
$CharPool += ([char[]](33..47))

Yes, I really counted them!

PowerShell Count Characters

Next, I needed to work out how to get a number of random characters from the pool so I used the Get-Random cmdlet.

$CharPool = ([char[]](97..122))
Get-Random -InputObject $CharPool -Count 5

The problem with this though is that each of the characters are on a new line.


Then I remembered the -Join operator.

(Get-Random -InputObject $CharPool -Count 5) -join ''

PowerShell Get-Random Join


About Andrew Barnes

A Scripting and Deployment Specialist.
This entry was posted in PowerShell and tagged , , . Bookmark the permalink.

10 Responses to PowerShell: New-Password Function

  1. Gianluigi says:

    Happy to read you again Andrew and congrats for your baby boy!


  2. Nice to hear you! Waiting for your posts. Thank you for sharing your knowlege


  3. keithga says:

    Thanks for sharing, however, note that some systems may require a level of complexity requirements, https://technet.microsoft.com/en-us/library/hh994562(v=ws.10).aspx

    Including the requirement that the password contains characters from *THREE* different categories. Since the function above is random, there is a chance that it may return a password that is not evenly distributed across more than three categories.


    • Thanks Keith. I initially wrote this because I wanted to generate 30 character passwords for various MDT and SCCM user based service accounts. I’m leaning more towards Group Managed Service Accounts these days.


  4. Congratulations! Enjoy you quality time with your son! I also wrote a blog about generating random passwords, maybe we can swap notes… http://wp.me/p4P7Dh-L Keep up the good work!


  5. Ejaz Hayder says:

    hey Andrew- Long time! Hope you’re well and big congrats with your baby Son


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s