PowerShell – Create directories and files for demonstrations

Sometimes, when I demonstrate the ease and strength of PowerShell to a customer, I need to quickly generate some fake files en directory structure… also those files have to be from different sizes. For this, I’ve created the following script:

function New-DemoData ($Path)
  # Usage: New-DemoData “D:\Company”
  1..10 | % { Set-Location -Path (New-Item -ItemType directory -path "$path\$_").fullname -ErrorAction SilentlyContinue
  for ( $i=1; $i -le (Get-Random -Minimum 10 -Maximum 200); $i++ )
      fsutil file createnew ("File" + $i + ".txt") (Get-Random -Minimum 1024 -Maximum 819200)

This script will create 10 folders (named 1-10) at the target path (provided by the parameter) and create a random number of *.txt files (between 10 and 200) in each directory, each file between 1KB and 800KB in size.

Leave a Reply

Your email address will not be published. Required fields are marked *