---
title: "Unlock Random Password Generation with PowerShell"
description: "Generate ultra-secure passwords in seconds. Make password management stress-free with PowerShell."
canonical: "https://adamtheautomator.com/random-password-generator/"
---

# Unlock Random Password Generation with PowerShell

> Generate ultra-secure passwords in seconds. Make password management stress-free with PowerShell.

Source: https://adamtheautomator.com/random-password-generator/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Unlock Random Password Generation with PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea553.jpg)

# Unlock Random Password Generation with PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)20 June 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Building the Script](#h-building-the-script)
*   [Building Random Password Length](#h-building-random-password-length)
*   [Building a PowerShell Function](#h-building-a-powershell-function)

It’s important to keep passwords long and not easily guessed. You might know how to create a secure password but wouldn’t it be nice to have a little script to do it for you? Lucky for you, if you’re on Windows, you can build a random password generator with [PowerShell](https://adamtheautomator.com/tag/powershell/) that will generate various lengths and complexity!

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Instead of building your own random password generator, instead just use an existing method that Microsoft already provides called the `GeneratePassword()` .NET method. This method comes with the `System.Web.Security.Membership` class and will do everything you need.

## Building the Script

To build this simple script, you’ll first need to make the `System.Web` assembly available. The `System.Web.Security.Membership` class is part of this assembly and it is not available by default.

```powershell
Add-Type -AssemblyName 'System.Web'
```

Once the `System.Web` assembly is available, you can now invoke the `GeneratePassword()` method.

The `GeneratePassword()` method has two arguments; `length` and `numberOfNonAlphanumericCharacters`. Using these two arguments allows you to create all kinds of random passwords with PowerShell.

*   Length – This is the length of characters the password will be.
*   numberOfNonAlphanumericCharacters – This is the number of non-alphanumeric characters that the method will generate. Think characters such as @,%,&, etc.

First, define the length of password you’d like to have. The below example is setting a variable of 10 which you’ll pass to the method.

```powershell
$length = 10
```

Next, define the number of non-alphanumeric characters you’d like to include in the password. The below code snippet sets a variable 5 which you’ll provide to the method to ensure five non-alpha characters are in the password.

```powershell
$nonAlphaChars = 5
```

Next is when you will call the `GeneratePassword()` method passing in the values of both of the variables defined above.

```powershell
[System.Web.Security.Membership]::GeneratePassword($length, $nonAlphaChars)
```

When you execute the above code snippet, PowerShell will return a random assortment of characters in a string which you can then use wherever you’d like.

## Building Random Password Length

You can even get a bit more random and make the password lengths random as well. Use the `Get-Random` cmdlet to come up with a random integer which you can then use as the `length` argument generating a random length password also!

```powershell
$minLength = 5 ## characters
$maxLength = 10 ## characters
$length = Get-Random -Minimum $minLength -Maximum $maxLength
$nonAlphaChars = 5
$password = [System.Web.Security.Membership]::GeneratePassword($length, $nonAlphaChars)
```

If you’ll be using this password in PowerShell, many components require a secure string. Once you have the password as a plain-text string, you can then convert it to a secure string using the `ConvertTo-SecureString` cmdlet.

```powershell
$secPw = ConvertTo-SecureString -String $password -AsPlainText -Force
```

## Building a PowerShell Function

Finally, take this random password generator to the next level by creating a function you can use wherever you want without having to remember all of the syntaxes explained above.

```powershell
function New-RandomPassword {
    param(
        [Parameter()]
        [int]$MinimumPasswordLength = 5,
        [Parameter()]
        [int]$MaximumPasswordLength = 10,
        [Parameter()]
        [int]$NumberOfAlphaNumericCharacters = 5,
        [Parameter()]
        [switch]$ConvertToSecureString
    )
    
    Add-Type -AssemblyName 'System.Web'
    $length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength
    $password = [System.Web.Security.Membership]::GeneratePassword($length,$NumberOfAlphaNumericCharacters)
    if ($ConvertToSecureString.IsPresent) {
        ConvertTo-SecureString -String $password -AsPlainText -Force
    } else {
        $password
    }
}
```

Once you’ve added this function to your profile, a [PowerShell module](https://adamtheautomator.com/powershell-modules/ "PowerShell module") perhaps or just copying/pasting it into your current session, you can execute it easily.

```powershell
New-RandomPassword -MinimumPasswordLength 10 -MaximumPasswordLength 15 -NumberOfAlphaNumericCharacters 6 -ConvertToSecureString
```

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Frandom-password-generator%2F&text=Unlock%20Random%20Password%20Generation%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Frandom-password-generator%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Frandom-password-generator%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
