It's important to keep passwords long and not easily guessed. Lucky for you, we have PowerShell to create a random password.
Instead of coming up with your own solution using
Get-Random, you can leverage the already-created and probably better
GeneratePassword() method on the
System.Web.Security.Membership class to create a random password one for you.
GeneratePassword() method has two arguments;
numberOfNonAlphanumericCharacters. Using these two arguments allows you to create all kinds of random passwords with PowerShell.
First, you'll probably need to let PowerShell know about the System.Web assembly since it's not loaded by default using
Add-Type -AssemblyName 'System.Web'. Next, simply call the
GeneratePassword() static method like this:
$length = 10 ## characters $nonAlphaChars = 5 [System.Web.Security.Membership]::GeneratePassword($length, $nonAlphaChars)
This is bound to return some crazy passwords your users will love!
But this isn't random, you say? Not a problem. Using the
Get-Random command, we can make these passwords as random as you want.
$minLength = 5 ## characters $maxLength = 10 ## characters $length = Get-Random -Minimum $minLength -Maximum $maxLength $nonAlphaChars = 5 $password = [System.Web.Security.Membership]::GeneratePassword($length, $nonAlphaChars)
The code above will return a random, plain-text password. You can then convert this to a securestring if you'd like.
$secPw = ConvertTo-SecureString -String $password -AsPlainText -Force
Subscribe to Adam the Automator
Get the latest posts delivered right to your inbox