Skip to main content

Simple script to generate a new secure password where you only need to specify the length. The password will be shown using write-host and automatically copied to the clipboard.

<#
.SYNOPSIS
    Just a quick simple script to generate a new secure password.

.DESCRIPTION
    Simple script to generate a new secure password where you only need to specify the length.
    The password will be shown using write-host and automatically copied to the clipboard.

.PARAMETER Length
    Enter the password length required.
    For example: 16

.EXAMPLE
    New-SecurePassword.ps1 -Length 16

 .NOTES
    Version: 1.0
    Author: Maarten Peeters
    Creation Date: 31-07-2018
    Purpose/Change: Fast creation of a new password
#>
param(
    [Parameter()]
    [int] $Length = 16
)

$Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) | Sort-Object {Get-Random})[0..($Length - 1)] -join ''
Write-Host 'Password copied to clipboard' -ForegroundColor Green
Set-Clipboard -Value $Password

# -------------------------------------------

<#
.SYNOPSIS
    Generate a password of the specified length.

.LINK
    https://thesmashy.medium.com/helpful-functions-for-your-powershell-profile-9fece679f4d6
#>
function Get-Pass
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [int] $Length = 20
    )

    -join (48..57 + 65..90 + 97..122 | ForEach-Object {[char]$_} | Get-Random -C $Length)
}