Powershell

How to Create a File Using PowerShell

PowerShell is a tool utilized to automate tasks for any operating system, including Windows. It supports file management operations such as creating, deleting, copying, and moving files. In PowerShell, you can create a file by using PowerShell’s built-in New-Item cmdlet.

If you want to know how to create a file using PowerShell, then this tutorial will be helpful for you!

How to Create a File Using PowerShell?

In PowerShell, the New-Item cmdlet is used to create single or multiple files of different types, such as text files, word documents, and others.

Syntax
Use the below-given syntax of the New-Item command to create a file:

> New-Item [-Path] <path>

Here, -Path refers to the location where the created file will be placed

Now, let’s check out some examples of creating single and multiple files using the New-Item PowerShell command.

Creating a single file using PowerShell

To create a single file using PowerShell, use the New-Item cmdlet and specify the file path.

Here, you can see a folder named Creatingfiles, which is currently empty. The file we are going to create will be placed in this folder:

Run the below command in PowerShell to create a text file named PSfile with the .txt extension:

> New-Item -Path "E:\Powershell\CreatingFiles\PSfile.txt"

After executing the given command, the file is created in the E:\Powershell\CreatingFiles folder:

For verification, we will open the E:\Powershell\CreatingFiles folder, and you can see the created PSfile is saved here:

Suppose you want to create a file of any other format like a word document. To do so, specify the extension in Path as .doc with the file name:

> New-Item -Path "E:\Powershell\CreatingFiles\PSDocument.doc"

For verification, open the E:\Powershell\CreatingFiles folder and view the document:

Want to create multiple files using PowerShell? Check out the following section!

Creating multiple files using PowerShell

In PowerShell, multiple files can be created at a time by utilizing the New-Item cmdlet:

> New-Item -Path "E:\Powershell\CreatingFiles\PSDocument.doc", "E:\Powershell\CreatingFiles\PSTextfile.txt"

The above-given command will create a PSTextfile.txt and a PSDocument.doc, simultaneously:

To verify if the added multiple files are created not, simply open the E:\Powershell\CreatingFiles folder and view files list:

We have provided all of the relevant information related to creating files using PowerShell.

Conclusion

To create a file in PowerShell, you can use the New-Item cmdlet. Its syntax is given as New-Item [-Path] <path>, where -Path refers to the location where the created file will be placed. By changing the file extension, you can create any type of file, such as a text or word document. The New-Item PowerShell command also permits you to create single and multiple files. In this tutorial, we discussed how to create a file using PowerShell with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.