I was recently taking a training course and the lab guide said “create some files in c:\stuff”.

imageSure, I could use Windows Explorer or Command Prompt or even a PowerShell command to do this.  I guess the easiest way would be to use Windows Explorer, select a file, copy it (Ctrl+C), then paste it multiple times (Ctrl+V [repeat until your fingers are tired]).

But hey, I’m a geek and I like to script stuff (yes, for fun).  But I’m not stupid so I didn’t create a script from scratch.  Stéphane van Gulick, a PowerShell MVP, has a nice script at http://powershelldistrict.com/create-files/

However, I like to tinker and can’t leave good enough alone so I forked it and added some additional functionality.

I give you… Create-RandomFiles.ps1 with the following changes

  • Added a Progress indicator
  • files are created with different sizes
  • TotalSize defaults to MB
  • added optional name prefix
  • added execution statistics
  • replaced fsutil.exe with New-Object byte[]

[read-more-redirect urltext=”CatapultSystems.com” url=”https://www.catapultsystems.com/blogs/powershell-create-randomfiles-ps1″]

Function Create-RandomFiles{
	Generates a number of dumb files for a specific size.
	Generates a defined number of files until reaching a maximum size.
	Specify the total size you would all the files combined should use on the harddrive.
	This parameter accepts the following size values (KB,MB,GB,TB).  MB is assumed if no designation is specified.
.PARAMETER NumberOfFiles
	Specify a number of files that need to be created. This can be used to generate 
	a big number of small files in order to simulate User backup specefic behaviour.
    This parameter is not mandatory, but the following choices are valid to generate files with the associated extensions:
        Multimedia : ".avi",".midi",".mov",".mp3",".mp4",".mpeg",".mpeg2",".mpeg3",".mpg",".ogg",".ram",".rm",".wma",".wmv"
		Image      : ".gif",".jpg",".jpeg",".png",".tif",".tiff",".bmp",".dib",".wmf",".emf",".emz",".svg",".svgz",".dwg",".dxf",".crw",".cr2",".raw",".eps",".ico",".pcx"
        Office     : ".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx"
		Junk       : ".tmp",".temp",".lock"
		Archive    : ".zip",".7z",".rar",".cab",".iso",".001",".ex_"
		Script     : ".ps1",".vbs",".vbe",".cmd",".bat",".php",".hta",".ini",".inf",".reg",".asp",".sql",".vb",".js",".css",".kix",".au3"
	If Filestypes parameter is not set, by default, the script will create all types of files.
    Specify a path where the files should be generated.
    Optional.  Allows prepending text to the beginning of the generated file names so they can be easily found and sorted.
    Permits to launch this script in "draft" mode. This means it will only show the results without really making generating the files.
    Allow to run the script in verbose mode for debbuging purposes.
   .\Create-RandomFiles.ps1 -TotalSize 1GB -NumberOfFiles 123 -Path $env:Temp -FilesTypes 'Office' -NamePrefix '~'
   Generate in the user's temp folder 123 randomly named office files all beginning with "~" which total 1GB.
   .\Create-RandomFiles.ps1 -TotalSize 50 -NumberOfFiles 42 -Path C:\Users\administrator\documents

   Generate in the adminstrator's documents folder 42 randomly named files which total 50MB.
    -Author: Stéphane van Gulick
    -Email : Svangulick@gmail.com
    -Version: 1.0
        -Creation V0.1 : SVG
        -First final draft V0.5 : SVG
        -Corrected minor bugs V0.6 : SVG
        -Functionalized the script V0.8 : SVG
        -Simplified code V1.0 : SVG

    ===== Change History =====
	based on http://powershelldistrict.com/create-files/
    -Author: Chad Simmons
    -2015/12/04: added Write-Progress, files are created with different sizes, TotalSize defaults to MB, added name prefix, added execution statistics, replaced fsutil.exe with New-Object byte[], added additional filetypes
    $StartTime = (get-date)
    $TimeSpan = New-TimeSpan -Start $StartTime -end $(Get-Date) #New-TimeSpan -seconds $(($(Get-Date)-$StartTime).TotalSeconds)
    $Progress=@{Activity = "Create Random Files..."; Status="Initializing..."}
    Write-verbose "Generating files"
    $AllCreatedFilles = @()
    function Create-FileName {
        begin {
			$AllExtensions = @()
			$MultimediaExtensions = ".avi",".midi",".mov",".mp3",".mp4",".mpeg",".mpeg2",".mpeg3",".mpg",".ogg",".ram",".rm",".wma",".wmv"
			$ImageExtensions      = ".gif",".jpg",".jpeg",".png",".tif",".tiff",".bmp",".dib",".wmf",".emf",".emz",".svg",".svgz",".dwg",".dxf",".crw",".cr2",".raw",".eps",".ico",".pcx"
			$OfficeExtensions     = ".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx"
			$OfficeExtensions2    = ".rtf",".txt",".csv",".xml",".mht",".mhtml",".htm",".html",".xps", `
									".dot",".dotx",".docm",".dotm",".odt",".wps", `
									".pot",".potx",".pptm",".potm",".pps",".ppsx",".ppsm",".odp", `
			$OfficeExtensions    += $OfficeExtensions2
			$JunkExtensions       = ".tmp",".temp",".lock"
			$ArchiveExtensions    = ".zip",".7z",".rar",".cab",".iso",".001",".ex_"
			$ScriptExtensions     = ".ps1",".vbs",".vbe",".cmd",".bat",".php",".hta",".ini",".inf",".reg",".asp",".sql",".vb",".js",".css",".kix",".au3"
			$AllExtensions        = $MultimediaExtensions + $ImageExtensions + $OfficeExtensions + $JunkExtensions + $ArchiveExtensions + $ScriptExtensions
			$extension = $null
			Write-Verbose "Creating file Name"
			switch ($filesType) {
				"Multimedia" {$extension = $MultimediaExtensions | Get-Random}
				"Image"      {$extension = $ImageExtensions | Get-Random}
				"Office"     {$extension = $OfficeExtensions | Get-Random }
				"Junk"       {$extension = $JunkExtensions | Get-Random}
				"Archive"    {$extension = $ArchiveExtensions | Get-Random}
				"Script"     {$extension = $ScriptExtensions | Get-Random}
				default      {$extension = $AllExtensions | Get-Random }
			Get-Verb | Select-Object verb | Get-Random -Count 2 | %{$Name+= $_.verb}
			$FullName = $NamePrefix + $name + $extension
			Write-Verbose "File name created : $FullName"
			Write-Progress @Progress -CurrentOperation "Created file Name : $FullName"
        end {
			return $FullName
process {
	If ($TotalSize -match '^\d+$') { [string]$TotalSize += "MB" } #if TotalSize isNumeric (did not contain a byte designation, assume MB
    $Progress.Status="Creating $NumberOfFiles files totalling $TotalSize"
    Write-Progress @Progress
    Write-Verbose "Total Size is $TotalSize"
    $FileSize = $TotalSize / $NumberOfFiles
    $FileSize = [Math]::Round($FileSize, 0)
    Write-Verbose "Average file size of $FileSize"
    $FileSizeOffset = [Math]::Round($FileSize/$NumberOfFiles, 0)
    Write-Verbose "file size offset of $FileSizeOffset"
    $FileSize = $FileSizeOffset*$NumberOfFiles/2
    Write-Verbose "Beginning file size of $FileSize"
    while ($FileNumber -lt $NumberOfFiles) {
        If ($FileNumber -eq $NumberOfFiles) { 
            $FileSize = $TotalSize - $TotalFileSize
            Write-Verbose "Setting last file to size $FileSize"
        $TotalFileSize = $TotalFileSize + $FileSize
        $FileName = Create-FileName -filesType $filesType
        Write-Verbose "Creating : $FileName of $FileSize"
        $Progress.Status="Creating $NumberOfFiles files totalling $TotalSize.  Run time $(New-TimeSpan -Start $StartTime -end $(Get-Date))"
        Write-Progress @Progress -CurrentOperation "Creating file $FileNumber of $NumberOfFiles : $FileName is $FileSize bytes." -PercentComplete ($FileNumber/$NumberOfFiles*100)

        $FullPath = Join-Path $path -ChildPath $FileName
#        Write-Verbose "Generating file : $FullPath of $Filesize"
            #fsutil.exe file createnew $FullPath $FileSize | Out-Null
            $buffer=New-Object byte[] $FileSize  #http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/09/hey-scripting-guy-how-can-i-use-windows-powershell-2-0-to-create-a-text-file-of-a-specific-size.aspx
        $FileCreated = ""
        $Properties = @{'FullPath'=$FullPath;'Size'=$FileSize}
		$FileCreated = New-Object -TypeName psobject -Property $properties
        $AllCreatedFilles += $FileCreated
        Write-verbose "$($AllCreatedFilles) created $($FileCreated)"
        Write-Progress @Progress -CurrentOperation "Creating file $FileNumber of $NumberofFiles : $FileName is $FileSize bytes.  Done." -PercentComplete ($FileNumber/$NumberOfFiles*100)

   		$FileSize = ([Math]::Round($FileSize, 0)) + $FileSizeOffset
    Write-Output $AllCreatedFilles
    Write-Output "Start     time: $StartTime"
    Write-Output "Execution time: $(New-TimeSpan -Start $StartTime -end $(Get-Date))" #http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/15/use-powershell-and-conditional-formatting-to-format-time-spans.aspx



PowerShell Create-RandomFiles.ps1
Tagged on:
%d bloggers like this: