Create dummy files with Powershell

Standard

The problem

I had to test a certain script where a lot of files needed to be copied. One problem I didn’t have a large set of files. So how can I create dummy files with Powershell.

The solution

The solution is the script below where you enter the following parameters

  • amount: Amount of files to be created
  • size: The size of the files. Can be inserted as 100kb, 1mb, 1gb
  • folder: The folder to create the files in
  • $name: Name of the files. The name will be the value of this parameter with a number
  • $extension: The extension of the file. Insert the value without the “.”
    param(
    $amount = $(throw "Please give an amount of files to be created")
    , $size = $(throw "Please give a the size of the files")
    , $folder = $(throw "Please give an output folder wehere the files need to be created")
    , $name = $null
    , $extension = $null
    )
    CLS
    # Check for input
    if(Test-Path $folder)
    {
    if($name -eq $null)
    {
    Write-Host "No filename given. Using default setting 'dummy'" -ForegroundColor Yellow
    $name = 'dummy'
    }
    
    if($extension -eq $null)
    {
    Write-Host "No filename extension given. Using default setting '.txt'" -ForegroundColor Yellow
    $extension = 'txt'
    }
    elseif($extension -contains '.')
    {
    $extension = $extension.Substring(($extension.LastIndexOf(".") + 1), ($extension.Length - 1))
    }
    
    for($i = 1; $i -le $amount; $i++)
    {
    $path = $folder + '\' + $name + '_' + $i + '.' + $extension
    $file = [io.file]::Create($path)
    $file.SetLength($size)
    $file.Close
    sleep 0.5
    }
    
    }
    else{
    Write-Host "The folder $folder doesn't exist" -ForegroundColor Red
    Exit(0)
    }

Example

.\Create-DummyFiles.ps1 -amount 20000 -size 50kb -folder 'F:\Temp\createfiles'

If you like this script please leave a comment.

Leave a Reply

Your email address will not be published.