New-SecurePassword.ps1


<#PSScriptInfo
.VERSION 1.0
.GUID 2e286fce-c1be-4cd3-980d-20c9690a0b6f
.AUTHOR
 Maarten Peeters - SharePointFire - https://sharepointfire.com
.COMPANYNAME
 SharePointFire
.COPYRIGHT
.TAGS
 Password
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.RELEASENOTES
 Version 1.0: Original published version.
#>
 

<#
.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(mandatory=$true)]
    [string] $Length
)

try{
    $Password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) | Sort-Object {Get-Random})[0..($Length-1)] -join ''
    write-host "The password $($Password) has been copied to the clipboard" -ForegroundColor Green
    Set-Clipboard -Value $Password
}
catch{
    write-host "Error occurred: $($_.Exception.Message)" -foregroundcolor red
}