title: Fastest Way to Create a File With Random Data
body: <p>I have a requirement to create files of a specified size filled with random data.
  I can't use any third party tools to achieve this so all I have at my disposal is all  the Powershell commands.</p>
  
  <p>What I have here works well with small files ranging in size from 1 KB to a 30 KB.
  It doesn't scale well when the file becomes large.</p>
  
  <pre><code>function makeFile([String]$filename, [int]$SizeInKb) {
      $str  = ""
      $size = 29 * $SizeInKb
      if (-not (Test-Path $filename)) {
          New-Item $filename -ItemType File | Out-Null
          for ($i=1; $i -le $size; $i++) {
              # A GUID is 36 characters long
              # We will create a string 29*36 (1044) characters in length and
              # multiply it with $SizeInKb
              $str += [guid]::NewGuid()
          }
          write $str to a file barring the last ($SizeInKb * 20) + 2 characters. 
          $strip_length = ($SizeInKb * 20) + 2
          ("$str").Remove(0, $strip_length) | Out-File "$filename" -Encoding ascii
      }
  }
  </code></pre>
  
  <p>Is there a better way to create files with random data?
  I'm currently generating GUIDs and then writing them to a file .</p>