When I write PowerShell scripts, many times I require random file name, folder path, numbers and strings keys, few .net and native PowerShell v5 cmdlets helps me to generate random required information easily, it saves time and don't have to generate random names or number or even password manually

Strong Random Password Generator :Microsoft Powershell

Here I am using .net object class [system.IO.Path], to generate random file names, I can generate these names using Get-Date command as well and it is shown in the last, but when filename doesn't concern, I use below one liners to get my task without long coding. I am running each command twice to show it is generating different and random values. Use Test-Path to confirm filename does not exist.

This code gives me random file name with random extension name.

[System.IO.Path]::GetRandomFileName()

In this .net object class code, it provides me random file name with .tmp extension, with the current user's temp folder path. With New-Item cmdlet and ItemType File I easily create the file. To reuse this random file save it in PowerShell variable. Example use: Add multiple proxy addresses with Microsoft PowerShell in Active Directory Groups

[System.IO.Path]::GetTempFileName()

Below command doesn't generate any random string but provides current user's temp folder path.

[System.IO.Path]::GetTempPath()

Next command is about producing arbitrary GUID (Globally unique identifier). There is .net class way to create it, using below command.

[System.Guid]::NewGuid().ToString()

Another similar method to above command is [System.Math].GUID. If you are using PowerShell V5, there is native cmdlet available.

New-Guid

I found it's two of the use while creating site column with specific GUID in Microsoft sharepoint and in when configuring DSC (desired state configuration) pull server. Instead I am using simple example and creating new folder with Guid name.

New-Item -Name $([System.Guid]::NewGuid().ToString()) -Path C:\Temp\GUID\ -ItemType Directory

Above methods are good for temp files, folder creation or Guid, below cmdlet is general purpose and can be used in any case. When you run Get-Random it generates random incrementing number, same can be generated using .net object class method [Environment]::TickCount.

If you need a random number between minimum and maximum, DotNet object class [Int32]::MaxValue represents and shows the largest possible value of an Int32. use below command show random number between 1 to 100 (min and max)

Get-Random -Minimum 1 -Maximum 100

This command supports piping from result, here it shows the list of colors and choose random color name. Example use: POWERSHELL FUN SEND KEYS ON THE SCREEN

[ConsoleColor].GetEnumValues() | Get-Random

If you have array of list, use piping to get random string name.

'John', 'Yohan', 'Rick' | Get-Random

Although Get-Random is not true random. Given the identical input in seed, Get-Random will produce the similar output each single time. By default get-random takes its seed from the system time ([Environment]::TickCount). An attacker or hacker who identifies the seed and approximately what time the seed was generated can irrelevantly generate a short list of speculations which will contain the generated number. Microsoft does offer a true .net object class RNG (Randon Number Generator) -System.Security.Cryptography.RNGCryptoServiceProvider.

You can use this as random seed, with the following caution: given a specific seed, the behavior is still deterministic, so if you set it once, you can still only ever get 32 bits of entropy out, since that is the seed size.

Using the same seed generates same number. Setseed produces non-random behavior, It is used typically used only to reproduce results, such as when debugging or analyzing a script.

Get-Random -SetSeed 1

I have taken below code from Source on System.Security.Cryptography.RNGCryptoServiceProvider, Number is unpredictable.

function Get-Rng {

$RandomBytes = New-Object -TypeName "System.Byte[]" 4

$Random = New-Object -TypeName "System.Security.Cryptography.RNGCryptoServiceProvider"

$Random.GetBytes($RandomBytes)

[BitConverter]::ToInt32($RandomBytes, 0)

}

Get-Random -SetSeed (Get-Rng)

This is one of the code I use frequently to generate random string based on date, This is useful when generating logs in separately dated files or folder on even in log file.

$D = Get-Date

"{0}{1}{2}-{3}.{4}.{5}.{6}" -f $D.Day,$D.DayOfWeek,$D.Year,$D.hour,$D.Minute,$D.Second,$D.Millisecond