header image

Create a random string

Posted by: | December 27, 2018 Comments Off on Create a random string |

I often need to create a random string of characters. Here’s a simple function to achieve that:


function get-randomstring {
param (

$rca = 1..$length |
foreach {
$ran = Get-Random -Minimum 97 -Maximum 123

$rca -join ”



The required length is passed as an integer parameter. Use the range operator as a counter and in Foreach-Object generate a random number between 97 and 122. Convert that to a char (a=97 and z-122) and add to the array. Join the array of chars to create the string.

under: PowerShell

Comments are closed.