close

Home

PowerShell

How to Create Files and Folders with PowerShell

Mike Kanakos

|
PowerShell

When you need to create files and folders on a Windows PC, using the GUI seems the most intuitive way to do it. However, PowerShell can intelligently create multiple files and folders in bulk. This is a pretty common task for almost any IT professional, and in this guide, we’ll show you how to create your files and folders using the New-Item PowerShell cmdlet.

How to create a file in PowerShell

Once you understand how to use PowerShell, the speed at which you can create things makes mundane tasks much easier. Let’s dive in and look at how to create files and folders using the New-item, Add-Content, and Test-Path cmdlets.

How to create a file with the New-item command

Creating a new file with PowerShell can be done with the New-Item cmdlet. If I wanted to create a new text file named MyTextFile.txt, the syntax would be the following:

New-Item MyTextFile.txt
Creating a file using the New-Item cmdlet
Creating a file using the New-Item cmdlet

In the example above, PowerShell created a file in the c: directory. However, I can specify different a location simply by adding the path to the filename. Here’s an example below:

New-Item c:\tools\MyNewFile.txt
We can specify a different location simply by adding the path to the filename
Creating a file in a different folder

Notice that in the screen capture above, I was in the temp directory, but I created the file in the tools directory. Once you are familiar with creating a new file, it is only one more line of code to add to start creating files in bulk.

Here’s an example of how to use a simple ForEach loop to create 10 files, all with a similar file name.

(1..10) | ForEach-Object {New-item “MyNewFile$_.txt”}
We use a simple ForEach loop to create 10 files with a similar name
Create multiple files with a simple loop

If necessary, you can also use the Get-Childitem cmdlet to list all the files in that directory.

Create a file after checking that it doesn’t already exist

You can also use PowerShell to test if files already exist before you attempt to create them. There are several ways to test if files already exist with PowerShell, but the easiest one is to use the Test-Path cmdlet.

As the name implies, the Test-Path cmdlet can check if a path exists. You can check for files and/or folder paths. The cmdlet returns True if a file or folder exists, and False if it does not.

In the example below, I am checking to see if the file named MyTextFile.txt exists. Since the file exists, the Test-Path cmdlet returned True:

Test-Path MyTextFile.txt
Using test-Path to see if a file exists
Using test-Path to see if a file exists

Once you learn the logic for testing if a file exists, you can use the Test-Path command with an If statement to create a file when Test-Path returns false. I’ll show you how to do that in the example below:

  • Here, I am using a variable named $FileName for the file name to make my code easier to read and to avoid repetitive typing.
  • Next, I have added an If statement to check for a file. The round brackets contain the condition to test. The curly braces contain the code I want to execute if the test is true. Notice that the -not operator is in use in my example.
  • The code says that if the file named MyTextFile.txt does not exist, then the file should be created. An If statement only executes if the condition is true.
  • I have added the -not parameter to change the condition to match on False instead of True.
$Filename = "MyTextFile.txt"
if (-not(test-path $Filename)){new-item $FileName}
We use the Test-Path command with an If statement to create a file when the commands returns false
Checking if a file exists and creating a new file if it doesn’t exist

Let’s walk through the output together so we know exactly what happened: In example 1, I ran my code and nothing happened; notice there is no output. That’s because in my c: directory, a filename MyTextFile.txt already exists. Since it exists, the if statement did not execute the code.

In example 2, however, you can notice that I changed the directory from the temp directory to the tools directory. Since there’s no file named MyTextFile.txt in the latter, when I execute the code a second time, the if statement executes the New-Item cmdlet and creates a new file.

How to write data into a new or existing file

We’ve already looked at how to create new files with PowerShell. However, if you look at the previous screen captures, notice that the file size of all new files is 0 bytes. That’s because the New-Item cmdlet actually creates empty files.

If you want to add data to an existing file, you can use the Add-Content cmdlet. The syntax is easy to grasp, just like for the New-item command. Here’s an example below:

Add-Content mytextfile.txt -Value "Here's some sample text written via PowerShell"

Next, I can check my file using the Get-Content cmdlet.

get-content .\mytextfile.txt
We can add data to an existing file with the Add-Content cmdlet.
Adding content to an existing file

I can also open the file in PowerShell using the Invoke-Item cmdlet.

Invoke-Item .\mytextfile.txt
Opening a file with the Invoke-Item cmdlet
Opening a file with the Invoke-Item cmdlet

How to overwrite an existing file

If PowerShell can be used to create new files, we can also use it to replace existing files in a folder. PowerShell will warn you if you try to create a file while one already exists with the same name.

You can see in the example below the error message PowerShell displays when I attempt to create a file named MyNewFile3.txt.

New-item .\MyNewFile3.txt
PowerShell warns of an existing file with the same name
PowerShell warns of an existing file with the same name

However, if I wish to replace that file, I can do that by adding the -force parameter, which will overwrite any existing file with the same name.

new-item .\MyNewFile3.txt -Force
we use the new-item cmdlet with the -force parameter
Overwriting a file using the -force parameter

How to create a folder in PowerShell

We just went through the different ways to create files in PowerShell. Now, let’s see how to create folders with the New-item and md commands.

Create a folder with the New-item command

Creating folders with PowerShell is also an easy task with the same New-Item cmdlet we used previously to create files. All we need is to specify the -ItemType parameter.

The -ItemType parameter controls whether the New-Item cmdlet creates a file, directory (folder), symbolic link, junction point, or a hard link. You can find out more about these options on the Microsoft Docs page for New-Item.

Creating a new folder with PowerShell is similar to creating a new file. Here’s an example with the command below:

new-item TestFolder1 -ItemType Directory
Creating a folder using the New-Item cmdlet
Creating a folder using the New-Item cmdlet

Create a folder with the md command

PowerShell is now a cross-platform language, which means it can be run on Windows, macOS, or Linux. The process of creating files and folders in Windows, Linux, and macOS from the command line is similar.

However, each OS has its own commands with different names for doing the same tasks. For example, to create a new folder called NewDirectory, each OS has a similar syntax with different cmdlets names: In DOS, the syntax is ‘MD NewDirectory’, but in Linux, it’s ‘MKDIR NewDirectory’.

Microsoft has taken many of the popular cmdlets names from other languages and included them in PowerShell. Commands such MKDIR or MD don’t actually exist in PowerShell: Instead, they’re aliases to their PowerShell brethren that do the same thing.

In PowerShell, MD is an alias to MKDIR. Also, MKDIR is an alias to the New-Item cmdlet. So that means if you type MD, MKDIR, or New-Item in PowerShell, all those commands have the same effect: running the New-Item cmdlet.

Here’s a quick example to highlight that functionality.

md Demofile1.txt
mkdir demofile2.txt
new-item demofile 3.txt
MD is an alias to MKDIR, and the latter is an alias to the New-Item cmdlet
Creating new files with PowerShell aliases

In the examples above, you can see that I created three files using the various aliases and cmdlet names for New-Item. The aliases all work exactly the same as typing New-Item.

Conclusion

PowerShell allows for many ways to create files and folders. The New-Item cmdlet makes it incredibly easy to create files and folders.

Microsoft has included aliases that refer to commands from popular operating systems like DOS and Linux in PowerShell to help admins feel right at home. We can also use loops to create files and folders at scale and logic statements like If to ensure that we only create files that don’t already exist.

We can also make sure that files that already exist don’t get overwritten by code unless we want them to be replaced. All these options allow us to build scripts to automate the mundane process of managing files and folders.

PowerShell is a very versatile tool, and now that you’ve learned how to create files and folders with it, you can check out our previous guide about using PowerShell to delete files and folders.

Article saved!

Access saved content from your profile page. View Saved