Home > PowerShell > Function: New-Password – Creating Passwords with PowerShell

Function: New-Password – Creating Passwords with PowerShell

PowerShell logo 256x256When creating new accounts, an admin needs to assign a password. We often then check the box to force a user to change their password when they logon for the first time. Some organizations will use a ‘default’ password for all new accounts. That’s fraught with security implications, and I’ve never recommended it. The downside is that you, as an admin, need to think up a password for each new account. I know how it is – you look around at things on your desk, items on the wall, looking for ideas. Then you have to make sure your super password meets your organizations password requirements, including length and complexity. Well, no more!

Enter New-Password. This function takes one simple input – length. It then spits out a password of said length, using upper and lower case letters, numbers, and punctuation, as well as a phonetic version. If you choose not to use some of the punctuation characters, feel free to just put a ‘#’ in front of that particular line.

function New-Password	{
	[CmdletBinding(SupportsShouldProcess=$true)]
	param(
		[int]$length
	)
	$password = -join ([Char[]]'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%$!#^{}<>' | Get-Random -count $length)
	Write-Host "`n$password`n" -ForegroundColor green
	# Write-Host
	ForEach ($character in [char[]]"$password"){
		[string]$ThisLetter = $character
		switch ($ThisLetter)	{
			a	{$ThisWord = "alpha"}
			b	{$ThisWord = "bravo"}
			c	{$ThisWord = "charlie"}
			d	{$ThisWord = "delta"}
			e	{$ThisWord = "echo"}
			f	{$ThisWord = "foxtrot"}
			g	{$ThisWord = "golf"}
			h	{$ThisWord = "hotel"}
			i	{$ThisWord = "india"}
			j	{$ThisWord = "juliett"}
			k	{$ThisWord = "kilo"}
			l	{$ThisWord = "lima"}
			m	{$ThisWord = "mike"}
			n	{$ThisWord = "november"}
			o	{$ThisWord = "oscar"}
			p	{$ThisWord = "papa"}
			q	{$ThisWord = "quebec"}
			r	{$ThisWord = "romeo"}
			s	{$ThisWord = "sierra"}
			t	{$ThisWord = "tango"}
			u	{$ThisWord = "uniform"}
			v	{$ThisWord = "victor"}
			w	{$ThisWord = "whiskey"}
			x	{$ThisWord = "xray"}
			y	{$ThisWord = "yankee"}
			z	{$ThisWord = "zulu"}
			1	{$ThisWord = "one"}
			2	{$ThisWord = "two"}
			3	{$ThisWord = "three"}
			4	{$ThisWord = "four"}
			5	{$ThisWord = "five"}
			6	{$ThisWord = "six"}
			7	{$ThisWord = "seven"}
			8	{$ThisWord = "eight"}
			9	{$ThisWord = "nine"}
			0	{$ThisWord = "zero"}
			!	{$ThisWord = "!"}
			$	{$ThisWord = "$"}
			%	{$ThisWord = "%"}
			^	{$ThisWord = "^"}
			*	{$ThisWord = "*"}
			-	{$ThisWord = "-"}
			_	{$ThisWord = "_"}
			:	{$ThisWord = ":"}
			`;	{$ThisWord = ";"}
			`{	{$ThisWord = "{"}
			`}	{$ThisWord = "}"}
			`/	{$ThisWord = "/"}
			`<	{$ThisWord = "<"}
			`>	{$ThisWord = ">"}
			`#	{$ThisWord = "#"}
			`{	{$ThisWord = "{"}
			`}	{$ThisWord = "}"}
		}
		if ($ThisLetter -cmatch $ThisLetter.ToUpper()){
			$ThisWord = $ThisWord.ToUpper()
		}
		Write-Host "$ThisWord " -NoNewLine -ForegroundColor yellow
	}
	Write-Host "`n"
} # end function New-Password

Now, stick that function in your PowerShell profile. Each time you need a new password, use

New-Password -length [number]

such as

New-Password -length 12

And you now have a password to use.

New-Password

New-Password

 

  1. JPL
    December 31, 2011 at 9:39 pm | #1

    It’s a nice function to have on hand. I notice it won’t repeat the same character twice. Can you confirm my finding and how would you address it if we want to allow some repeating character in a password.

    • Pat Richard
      December 31, 2011 at 10:46 pm | #2

      I hadn’t noticed the non-repeating characters. I’ll take a look.

  1. No trackbacks yet.

Leave a Reply