Random Archives - i Love PowerShell https://ilovepowershell.com/tag/random/ I came. I saw. I automated. Tue, 02 May 2023 10:19:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://ilovepowershell.com/wp-content/uploads/2020/09/SiteLogo-150x150.png Random Archives - i Love PowerShell https://ilovepowershell.com/tag/random/ 32 32 The Random Password Generator for PowerShell Core 6 and 7 https://ilovepowershell.com/powershell-modern/the-random-password-generator-for-powershell-core-6-and-7/ Fri, 20 Aug 2021 03:56:02 +0000 https://ilovepowershell.com/?p=3114 What works for Windows Powershell, doesn't always work for .NET 5, which is what modern versions of PowerShell like PowerShell 7 use.

Well, thankfully it's pretty easy to write a simple random password generator in PowerShell 7

It follows a simple pattern, and I'll show you how to do it.

The post The Random Password Generator for PowerShell Core 6 and 7 appeared first on i Love PowerShell.

]]>
I’ve mentioned previously about How to Create a Random Password using the .NET Framework.

That works for Windows Powershell, but it doesn’t work for .NET 5, which is what modern versions of PowerShell use.

So what can you do?

Well, thankfully it’s pretty easy to write a simple random password generator in PowerShell 7. It follows a simple pattern, and I’ll show you how to do it.

  • Make a list of characters
  • Decide how long the password is going to be
  • Use a For-Loop to add characters to an array

Make your list of characters

You have a few options here. You could do something like this:

# Take a string of acceptable characters and change it into an array of individual characters
 [char[]]"abcdefABCDEF1234567890"

You could also use the RANGE selector (..) to help you

'a'..'z'

If you want to use multiple RANGES, you can do that too. Ranges can be added together to form a bigger list:

'a'..'z' + 'A'..'Z' + '0'..'9'

I actually wanted to include some special characters in my list, but I didn’t want to have any special characters that might cause problems when used in a SQL Server password.

Showing the list of ASCII characters

There are 255 ASCII characters. Some of them (like ♣ or ?) would not be useful to me for a password.

But a quick list like this would help me to find the ranges that I want to include for my password generator.

0..255 | Foreach-Object {"$_ : $([char]$_)"}
#In English, this is:
#  Give me a list of numbers from 0 to 255.
#  Then for each of those numbers,  write this to the screen:
#   The Number, a colon, and the ASCII character equivalent for that number  

After seeing the characters in a list, and checking to make sure I wasn’t going to use any special characters that would cause me problems later, this is what I ended up with.

$charlist = [char]94..[char]126 + [char]65..[char]90 +  [char]47..[char]57
# All uppercase and lowercase letters, all numbers and some special characters. 

No matter which way you create your list, you should end up with a variable to hold your characters. It should be a variable that is a character array.

$charList.GetType()

Should confirm that you have an array, not a string.

Got it so far? The rest is easy!

Decide how long you want your password to be

I’m not going to ever try to remember this password.

It’s mostly going to be:

  1. Generated
  2. Saved in an Azure KeyVault
  3. Looked up later when I need it

So I don’t really care what it is as long as it’s complex, long and impossible to guess.

But I don’t want it to even have the same number of characters each time.

I’ll have a range of password lengths. I want passwords to be at least 20 characters long, but it doesn’t need to be longer than 32.

There are a few logical ways to do that.

  1. A random number between 0 and 12, plus 20
  2. A random number between 20 and 32
light bulb

When I first wrote this, my brain was still thinking about “ranges” from character selection.

But there was a better way…

All of these lines do the same thing: They give me a random number between 20 and 32.

# Option 1:  Randomize the variation
$PwdLength = ([0..12] | Get-Random) + 20

# Option 2:  Randomization from the whole range
$PwdLength = [20..32] | Get-Random

#Option 3:  Built in parameters from a native PowerShell cmdlet.
$PwdLength = Get-Random -Minimum 20 -Maximum 32

Option 3 is best. It’s the easiest one to read.

Now make your random password from the list of characters

#Create a new empty array to store the list of random characters 
$pwdList = @()

# Use a FOR loop to pick a character from the list one time for each count of the password length
For ($i = 0; $i -lt $pwlength; $i++) {
  $pwdList += $charList | Get-Random
}

# Join all the individual characters together into one string using the -JOIN operator
$pass = -join $pwdList

The whole thing put all together

$charlist = [char]94..[char]126 + [char]65..[char]90 +  [char]47..[char]57
$pwLength = (1..10 | Get-Random) + 24  
$pwdList = @()
For ($i = 0; $i -lt $pwlength; $i++) {
   $pwdList += $charList | Get-Random
}
$pass = -join $pwdList

The post The Random Password Generator for PowerShell Core 6 and 7 appeared first on i Love PowerShell.

]]>
3114
Awesome and Simple Way to Generate Random Passwords with PowerShell https://ilovepowershell.com/windows-powershell-legacy/awesome-and-simple-way-to-generate-random-passwords-with-powershell/ Mon, 28 May 2018 21:34:52 +0000 https://ilovepowershell.com/?p=2621 I get tired of thinking up unique and strong passwords. So anymore, for the most part, I don’t do it. I will use random generated passwords, then save them into an Azure KeyVault. It is a good way to use strong passwords that are practically impossible to remember but then keep them usable and within […]

The post Awesome and Simple Way to Generate Random Passwords with PowerShell appeared first on i Love PowerShell.

]]>

I get tired of thinking up unique and strong passwords.

So anymore, for the most part, I don’t do it.

I will use random generated passwords, then save them into an Azure KeyVault.

It is a good way to use strong passwords that are practically impossible to remember but then keep them usable and within reach.

I’m also a big fan of randomly generating a password for service accounts.

By using PowerShell to create the service accounts, it’s even better because I never even know the passwords of the service accounts. I can have PowerShell:

  • generate a random password
  • save the password to the Key Vault, then
  • use that password for the service accounts.

It works like a dream!!!

But how do I generate the passwords?

It’s so simple that you’ll never use a homegrown, one-character-at-a-time randomizer again.

Hey…

This article was written before PowerShell Core came along and took away the [System.Web.Security.Membership] class.

This article still works if you’re on Windows PowerShell (through version 5) but if you’re on later PowerShell versions (like you should be)…

Here’s how to Make a Random Password in PowerShell 7

I use the built in .Net Framework GeneratePassword() method.

# GeneratePassword can be called as a static method
# You don't need to instantiate the class first. It works just fine like this:

[System.Web.Security.Membership]::GeneratePassword(24,5)
# It just takes two arguments: "How long is the password" and "How many special characters"?
The generated password is a string, and that's find for reading but not useful for creating a credential object. So you'll usually need to convert the password into a SecureString.

# Same as above, just a different password length and complexity
$PW = [System.Web.Security.Membership]::GeneratePassword(30,10)

# The password as it is now:
$PW

# Converted to SecureString
$SecurePass = $PW | ConvertTo-SecureString -AsPlainText -Force

# The SecureString object
$SecurePass

So that’s all there is to it! If you’ve got questions or comments, I’d love to hear them.

How did this work for you and what else have you tried?

The post Awesome and Simple Way to Generate Random Passwords with PowerShell appeared first on i Love PowerShell.

]]>
2621