Wednesday, August 12, 2015

Everyday Powershell - Part 33 - New-RandomPassword

Everyday Powershell returns! This series is about providing easily digestible chucks of powershell goodness so those of us new to the world or the shell can start leveraging the awesome power of the shell!

If you are the kind of person who creates a lot of accounts for any reason you know the value of having a password randomiser handy.

function new-randompassword{
    param(
        [Parameter(mandatory=$false,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
        [int]$length = 25
    )

    $possiblecharacters = "1","2","3","4","5","6","7","8","9","0","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","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","Q","R","S","T","U","V","W","Y","X","Z"
    $password = ""
    $i = 0
    While($i -le $length){
        $password += get-random -InputObject $possiblecharacters
        $i++
    }
    $password
}

Really straight forward and easy to understand function here with one parameter -length. Easily extended to support more characters. You could even remove possible similar looking characters from the list 1, I, l  kind look the same as do 0,O and o. It's all up to you! That's the great thing with writing your own functions. If you don't like what it does you can just change it right there and make it do what you want!

Have a great day Powershellers!