Creating a file using graphical tools like File Explorer is extremely easy in Windows. However, you don’t have a lot of options and configurations you can set while doing so. Additionally, you may need to create multiple files at once or create similar files in multiple directories.
In such cases, using command line tools like PowerShell is the most effective method. You can also include the PowerShell cmdlets to create files in a PowerShell script to perform flexible and advanced file-handling tasks.
How to Create File in PowerShell Command?
The default cmdlet to create a file in PowerShell is the New-Item
cmdlet. You can also use other cmdlets, such as Out-File
, by using them in certain manners. However, they have some restrictions, and you can’t use them in all scenarios.
So, we will only be discussing how you can use the New-Item
cmdlet to create files for different purposes in this article. You can also use the alias ni
instead of New-Item
Creating New File
On PowerShell, enter the command New-Item -Path “File full path” -ItemType File
to create the file. For example, New-Item -Path “D:\New\test.txt” -ItemType File

Alternatively, you can assign the file path to a variable and use the variable in the -Path attribute. It is especially useful if you need to use this path multiple times in a script. For instance,
$FilePath = “D:\New\test.txt”
New-Item -Path $FilePath -ItemType File
You can create a file with any extension, not just .txt
You can also directly input some value or text into the file while creating it by using the -Value
attribute. For example, New-Item -Path “D:\New\test.txt” -ItemType File -Value “Test File”
Creating Multiple Files
You can use multiple file paths in the -Path
attribute separated by commas to create them simultaneously. For instance, New-Item -Path "D:\New\test.txt", "D:\New\test1.csv"
creates two files test.txt
and test1.csv
inside the D:\New\ folder

Creating File After Checking if It Exists
If a file is already present in the folder path, you can’t create a new file there. Additionally, trying to do so will create an exception and crash the script. So, if you need to create a file through a script, it’s always to check if a file already exists before writing the cmdlet to create it.
You can use the Test-Path
cmdlet to do so in the following way:
if (-not (Test-Path -Path “File full path”)){
New-Item -Path “File full path” -ItemType File
}
else{
//anything you want to do if the file already exists
}

And the same as for creating a new file, you can assign the path to a variable and use the variable in both instances instead.
You can also catch this exception by using Try and Catch statements.
Creating File by Overwriting Pre-existent File
You can also forcibly overwrite a file if it is already present in order to avoid the exception error. You need to use the -Force
attribute to do so. The syntax is:New-Item -Path “Full file path” -ItemType File -Force

Creating Files in Multiple Directories
It is also possible to create files in multiple directories at the same time by including all the directories inside the -Path attribute.
For instance, New-Item -Path "D:\New", "D:\New Folder", "D:\New folder (2)" -Name temp.txt -ItemType File
creates temp.txt
inside New, New Folder, and New Folder (2) folders in the D:
root directory.

You can also use wildcards to specify multiple folders. For example, New-Item -Path D:\* -Name temp.txt -ItemType File
creates temp.txt
in all files directly inside the D:
drive.
Creating Symbolic Link File
You can use the SymbolicLink
item type with this cmdlet to create a symbolic link pointing to a file.
For example, New-Item -ItemType SymbolicLink -Path "D:\New Folder\link" -Target "D:\New\temp.txt"
creates a symbolic link of D:\New\temp.tx
t inside D:\New Folder
with the name link.

It is also possible to create a hard link by using the -ItemType
HardLink attribute.