Generating Random password for user accounts in AD

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

install-module azsbtools
help New-Password -Full

NAME
    New-Password
    
SYNOPSIS
    Function to generate random password
    
    
SYNTAX
    New-Password [[-Length] <Int32>] [[-Include] <String[]>] [-CodeFriendly] [<CommonParameters>]
    
    
DESCRIPTION
    Function to generate random password
    

PARAMETERS
    -Length <Int32>
        Number between 2 and 256
        Default is 24
        
        Required?                    false
        Position?                    1
        Default value                24
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -Include <String[]>
        One or more of the following:
          UpperCase
          LowerCase
          Numbers
          SpecialCharacters
        Default is all 4
        
        Required?                    false
        Position?                    2
        Default value                @('UpperCase','LowerCase','Numbers','SpecialCharacters')
        Accept pipeline input?       false
        Accept 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 96
        
        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see 
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216). 
    
INPUTS
    
OUTPUTS
    
NOTES
    
    
        Function by Sam Boutros
        v0.1 - 27 July 2017
        v0.2 - 3 May 2020 - included in AZSBTools PS module
        v0.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 -Verbose
    
RELATED LINKS
    https://superwidgets.wordpress.com/category/powershell/
New-Password -Length 8 -Include Numbers,UpperCase,LowerCase
whi7X4dT

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. :slight_smile:

There are some simple solutions here. I like the 3 line regex solution.

https://stackoverflow.com/questions/2625150/membership-generate-password-alphanumeric-only-password

 

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:

-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.

Thank You Everyone, all these solutions worked great for me.