...Because someone's gotta tell the story...


To return to the main Blog List, click Full Blog Listing.


Site Updates

Random Character Generation, creating new files

Tuesday, June 11, 2013 in Powershell (Views: 3222)
So another quickie, showing the Get-Random cmdlet.

In this example, we are going to take a sample of characters and create a random file name from them. This can also be good for random password generation as well (although you may want to add special characters).

If you are big on capitalization, you can remove the upper or lower as you want from the $InclChars variable.

$Private:OFS = ""
$InclChars = "abcdefghkmnprstuvwxyzABCDEFGHKLMNPRSTUVWXYZ123456789"
$RandomChars = 1..8| ForEach-Object {Get-Random -Maximum $InclChars.length}
$RandomFileString = ([String]$InclChars[$RandomChars])+".txt"
write-host New File Name: $RandomFileString

I use this in concatenating files and creating new random file named output. But, it's good for any time you want to randomize.


Related Blogs You May Be Interested In:

To leave a comment, please log in and/or register.