Create test directory tree with PowerShell

I needed to create directory tree structure to test something and I thought that it might be the right time to try out PowerShell.

An hour ago I didn’t know PowerShell, and I still don’t — I’m pretty sure there this is noob script… Nonetheless it does what it meant to and is better than „hello world” for a first script 😉

function New-TestFS ([string]$Path = "c:\fs\test", [int]$Depth = 1) { 
	if ($Depth -gt 10) { 
	Write-Host "#### NEW DIR: $Path $Depth"
	New-Item -ItemType directory -path $Path -ErrorAction SilentlyContinue 
  	900..999 | % { 
		New-TestFS -Path ("$Path\Dir$_") ($Depth + 1)
	Write-Host "#### NEW FILES: $Path $Depth"
	for ( $i=1; $i -le (Get-Random -Minimum 100 -Maximum 199); $i++ ) { 
		Write-Host "FILE: $Path\File$i.txt"
		fsutil file createnew ("$Path\File$i.txt") 0

Kudos to Jeff Wouters for his powershell example 🙂

Leave a Reply

Back to Top