Welcome › Forums › General PowerShell Q&A › Generating Random password for user accounts in AD
- This topic has 4 replies, 4 voices, and was last updated 1 month ago by
Participant.
-
AuthorPosts
-
-
December 15, 2020 at 11:17 am #279888
Hi Everyone,
I am trying to generate a random password for user accounts in AD, Is there a way I can eliminate generating non alphanumeric characters?
Function GenerateNewPassword{
[System.Web.Security.Membership]::GeneratePassword(8,0)
$PasswordLenth = ‘8’
$NonAlphaNumeric = ‘0’
$SecurePassword = [System.Web.Security.Membership]::GeneratePassword($PasswordLenth,$NonAlphaNumeric)}
GenerateNewPassword -
December 15, 2020 at 11:47 am #279906PowerShell1install-module azsbtoolsPowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283help New-Password -FullNAMENew-PasswordSYNOPSISFunction to generate random passwordSYNTAXNew-Password [[-Length] <Int32>] [[-Include] <String[]>] [-CodeFriendly] [<CommonParameters>]DESCRIPTIONFunction to generate random passwordPARAMETERS-Length <Int32>Number between 2 and 256Default is 24Required? falsePosition? 1Default value 24Accept pipeline input? falseAccept wildcard characters? false-Include <String[]>One or more of the following:UpperCaseLowerCaseNumbersSpecialCharactersDefault is all 4Required? falsePosition? 2Default value @('UpperCase','LowerCase','Numbers','SpecialCharacters')Accept pipeline input? falseAccept wildcard characters? false-CodeFriendly [<SwitchParameter>]When set to True, this function excludes the following 4 characters from the 'SpecialCharacters' list of the password" ==> ASCII 34$ ==> ASCII 36' ==> ASCII 39` ==> ASCII 96Required? falsePosition? namedDefault value FalseAccept pipeline input? falseAccept wildcard characters? false<CommonParameters>This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable. For more information, seeabout_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).INPUTSOUTPUTSNOTESFunction by Sam Boutrosv0.1 - 27 July 2017v0.2 - 3 May 2020 - included in AZSBTools PS modulev0.3 - 19 October 2020 - Added Switch to remove 4 code unfriendly characters-------------------------- EXAMPLE 1 --------------------------PS C:\>New-Password-------------------------- EXAMPLE 2 --------------------------PS C:\>New-Password -Length 10 -Include LowerCase,UpperCase,Numbers -VerboseRELATED LINKShttps://superwidgets.wordpress.com/category/powershell/PowerShell12New-Password -Length 8 -Include Numbers,UpperCase,LowerCasewhi7X4dT
-
December 15, 2020 at 2:18 pm #279966
What I fail to understand is why the GeneratePassword method completely ignores the Non Alpha setting. If I say 0, then by god I want 0. 🙂
There are some simple solutions here. I like the 3 line regex solution.
https://stackoverflow.com/questions/2625150/membership-generate-password-alphanumeric-only-password
-
December 16, 2020 at 9:05 am #280203
Thank You Everyone, all these solutions worked great for me.
-
-
December 15, 2020 at 4:29 pm #280005
If you don’t know all the ASCII code ranges and don’t have PowerShell v6+, you can opt for an all inclusive ASCII range and use regex matching to filter it down. Then pipe to Get-Random for your length:
PowerShell1-join ([char[]](0..128) -match '\p{L}|\p{N}' | Get-Random -Count 8)The \p{unicodeProperty} syntax matches on unicode properties. L is letter and N is number.
-
-
AuthorPosts
- You must be logged in to reply to this topic.