Last active
          December 10, 2024 23:31 
        
      - 
      
 - 
        
Save steviecoaster/189269dd82a284f514aded0b0fab35b3 to your computer and use it in GitHub Desktop.  
    Cross platform password generator
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function New-RandomPass { | |
| <# | |
| .Synopsis | |
| Generates and returns a suitably secure password | |
| .EXAMPLE | |
| New-RandomPass | |
| Returns a random password as a SecureString object | |
| .EXAMPLE | |
| New-RandomPass -Length 128 | |
| Returns a random password whose length is 128 characters as a SecureStringObject | |
| .EXAMPLE | |
| New-RandomPass -AsPlainText | |
| Returns a random password as plain text | |
| #> | |
| [CmdletBinding()] | |
| [OutputType([System.Security.SecureString])] | |
| param( | |
| [Parameter()] | |
| [ValidateRange(1, 128)] | |
| [int]$Length = 64, | |
| [Parameter()] | |
| [char[]]$AvailableCharacters = @( | |
| # Specifically excluding $, `, ;, #, etc such that pasting | |
| # passwords into support scripts will be more predictable. | |
| "!%()*+,-./<=>?@[\]^_" | |
| 48..57 # 0-9 | |
| 65..90 # A-Z | |
| 97..122 # a-z | |
| ).ForEach{ [char[]]$_ }, | |
| [Parameter()] | |
| [Switch] | |
| $AsPlainText | |
| ) | |
| end { | |
| $NewPassword = [System.Security.SecureString]::new() | |
| while ($NewPassword.Length -lt $Length) { | |
| $NewPassword.AppendChar(($AvailableCharacters | Get-Random)) | |
| } | |
| if (-not $AsPlainText) { | |
| $NewPassword | |
| } | |
| else { | |
| [System.Net.NetworkCredential]::new($null, $NewPassword).Password | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment