How To create a file of specific size in Windows

Sometimes, to test a system (program interactions, email, filters), you need to create a file of a certain size. Windows has two standard tools for creating a custom size file.

Content

  1. Create file using command prompt
  2. Create file using PowerShell

Article in other languages:
?? – Cómo crear un archivo de tamaño específico en Windows
?? – Как создать файл определенного размера в Windows
?? – Comment créer un fichier de taille spécifique sous Windows
?? – So erstellen Sie eine Datei mit einer bestimmten Größe in Windows
?? – Hoe maak je een bestand met een specifieke grootte in Windows

Create file using command prompt

Use the fsutil command to create a file of the specified size using the Windows command prompt. The file size for this command is specified in bytes.

Create 1 MB TestFile.txt:

fsutil file createnew D:\TestFile.txt 1048576

If you get “Error: Access denied” when creating a file at the specified location – run command prompt as administrator.

Create a file of a specific size using the Windows command prompt. Error: Access is denied, while creating file
Error: Access is denied, while creating file

Create file using PowerShell

Create empty file of specified size

To create a file of a specific size with Windows PowerShell, use the following sequence of commands (file size is specified in: Kb, Mb, Gb):

$file = New-Object -TypeName System.IO.FileStream -ArgumentList D:\TestFile.txt,Create,ReadWrite
$file.SetLength(1Mb)
$file.Close()

Windows PowerShell will create an empty file (filled with NULL characters) of the selected size.

If, when creating a file in the specified location, you receive an error: “New-Object : Exception calling “.ctor” with “3” argument(s): “Access to the path … “- run Windows PowerShell as administrator.

Create a file of a specific size using Windows PowerShell. Error: Access to the path, when creating a file
Error: Access to the path, when creating a file

Create non-empty file of given size

To create a 1 MB non-empty file (random content), run the Windows PowerShell commands:

$array = New-Object -TypeName Byte[] -ArgumentList 2Mb
$obj = New-Object -TypeName System.Random
$obj.NextBytes($array)
Set-Content -Path D:\TestFile.txt -Value $array -Encoding Byte

File creation speed will depend on the specified file size and the specifications of your computer.

Create non-empty file of arbitrary size using Windows PowerShell
Create a non-empty file of arbitrary size using Windows PowerShell

Create multiple files of a given size

Create 5 non-empty files (random content) 1MB in size using Windows PowerShell:

$array = New-Object -TypeName Byte[] -ArgumentList 1Mb
$obj = New-Object -TypeName System.Random
$obj.NextBytes($array)
for ($i=1; $i -le 5; $i++) {Set-Content -Path D:\Test\TestFile$i.txt -Value $array -Encoding Byte}

The speed of files creation will depend on the specified files size and the characteristics of your computer.

Create multiple files of arbitrary size using Windows PowerShell
Creating multiple files of arbitrary size using Windows PowerShell

? How to create a file of a specific size has been discussed in this article. Now you can create a file or several files of arbitrary size for testing programs or services. However, if you run into any problems, do not hesitate to write in the comments. I will try to help.

1 thought on “How To create a file of specific size in Windows”

  1. What factors may influence the speed of file creation when using these methods, and how can users optimize the process for their specific needs?

    Reply

Leave a Comment