I\'d like to user Powershell to create a random text file for use in basic system testing (upload, download, checksum, etc). I\'ve used the following articles and co
Agree with @dugas that the bottleneck is calling Get-Random for every character.
You should be able to achieve nearly the same randomness if you increase your character array set, and use the -count property of Get-Random.
If you have V4, the .foreach method is considerably faster than foreach-object.
Also traded Out-File for Add-Content, which should also help.
# select characters from 0-9, A-Z, and a-z
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')
$chars = $chars * 126
# write file using 128 byte lines each with 126 random characters
(1..(1mb/128)).foreach({-join (Get-Random $chars -Count 126) | add-content testfile.txt })
That finished in about 32 seconds on my system.
Edit: Set-Content vs Out-File, using the generated test file:
$x = Get-Content testfile.txt
(Measure-Command {$x | out-file testfile1.txt}).totalmilliseconds
(Measure-Command {$x | Set-Content testfile1.txt}).totalmilliseconds
504.0069
159.0842