Search Tools Links Login

Generating Passwords with Powershell


To generate a secure password using PowerShell, you can use the New-RandomPassword cmdlet, which is part of the Microsoft.PowerShell.Utility module. Here's an example of how you can use this cmdlet:

# Import the Microsoft.PowerShell.Utility module
Import-Module Microsoft.PowerShell.Utility

# Generate a random password with 16 characters
$password = New-RandomPassword -Length 16

# Output the generated password
$password

This will generate a random password with 16 characters, using a combination of upper and lower case letters, numbers, and special characters. You can adjust the length of the password by specifying a different value for the -Length parameter.

It's important to note that the New-RandomPassword cmdlet is only available in PowerShell version 3.0 and above. If you're using an older version of PowerShell, you can use the Get-Random cmdlet to generate a random string of characters, and then use this string as a password.

Here's an example of how you can do this:

# Generate a random string with 16 characters
$password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 16 | ForEach-Object {[char]$_})

# Output the generated password
$password

This will generate a random password with 16 characters, using a combination of upper and lower case letters and numbers. You can adjust the length of the password by specifying a different value for the -Count parameter.

It's also a good idea to enforce password complexity requirements, such as requiring a minimum length and the use of at least one upper case letter, one lower case letter, one number, and one special character. You can use the Test-PasswordComplexity cmdlet, which is also part of the Microsoft.PowerShell.Utility module, to verify that a password meets these requirements. Here's an example of how you can use this cmdlet:

# Import the Microsoft.PowerShell.Utility module
Import-Module Microsoft.PowerShell.Utility

# Generate a random password with 16 characters
$password = New-RandomPassword -Length 16

# Test the password for complexity
Test-PasswordComplexity -Password $password

This will output True if the password meets the complexity requirements, and False if it does not. You can adjust the complexity requirements by using the -MinimumLength, -MinimumLowerCase, -MinimumUpperCase, -MinimumSpecial, and -MinimumDigits parameters.

About this post

Posted: 2022-12-27
By: dwirch
Viewed: 409 times

Categories

Tip

Tutorials

Security

Scripting

Powershell

PowerShell Code Cache

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.