How to Create Files & Folders – Using PowerShell (The Easy Way!) – New-Item, Directories

What We'll Learn:

Welcome!
In this short guide we’re going to learn how to create a number of items using our PowerShell console.

  • We will start by learning how to navigate to where we want to create our item and checking if a file or folder with the same name already exists.
  • We’ll then learn how to create folders from our current as well as different directories.
  • And finally we’ll learn how to create all sorts of files such as text files, scripts and shortcuts.

Lets get started!

Navigating to where we want to create our file/folder.

Before we start learning how to create our files or folders, we must first navigate to where we want them to be created at. Let’s say that we want to navigate into the desktop directory and create our folder or file there.

Upon opening  your PowerShell console, after the first few lines you will notice something that looks like this C:\Users\<Username>. <Username> Should be your computers username in your case. This location is called the current working directory and is where the PowerShell console is located now.

In case you see something that looks like this instead C:\WINDOWS\system32 that means that you opened a PowerShell console with administrator privileges, which are not necessary for what we will do in this article and can even be dangerous if you are a beginner, so open a regular PowerShell console, without admin privileges instead.

From this location we must use the Get-ChildItem cmdlet to view all the files and folders of the current directory. Here is what that would look like:

Get-Childitem

From the output we can see our desktop directory, to navigate there, let’s use the set-location cmdlet followed by the name of the directory we want to navigate into.

Set-Location Desktop

Your current working directory should change at once.

In case you want to navigate to a directory which contains a space within its name, you need to surround it in quotes. Here is an example:

Set-Location “C:\Program Files”

The quotes tell the PowerShell console that the spaces separating the words do not constitute an additional parameter or name and are just a continuation of the name of our directory. Simple enough right?

Incase you navigated to the wrong directory by mistake you can navigate backwards, or in other words navigate to your parent directory, by either entering the full location which in this case is C:\Users\<Username> or alternatively a nice shortcut is using two dots one after the other which will do the same.

Set-Location ..

You should now have managed to find your way into the directory in which you wan to create your fie or folder and are ready for the next section!

Checking if a file/folder already exists.

Just a little more preparation.

Its always a clever idea to check if a file or folder with the same name already exists in your target directory, so that you do not accidentally overwrite it.

To do so, we need to use the Test-Path parameter followed by our target directory, along with the name of the file or folder we want to create. In case you want to create a file you need to include an extension as well.

Here is an example with a text file.

Test-Path “TestFile.txt”

If the Test-Path cmdlet returns true, that means that a file with the same name already exists, if not, it doesn’t, and it is safe for us to create it.

The process is exactly the same when checking if a folder exists, simply replace “TestFile.txt” in the above example with the name of the folder you want to create within quotes.

To check if a file or folder exists in a directory other than your current one you need to enter its location rather than its name directly. Here is an example:

Test-Path “C:\Users\<Username>\Desktop\My Folder”

This command will check if a folder named “My Folder” exists in the desktop directory. Replace <Username> with your computers username.

Creating Folders/Directories using New-Item

Now, its time for the fun stuff! Let’s start with creating our new folder/directory.

Doing so its quite simple, all we have to do is type the following command:

New-Item -Name “My Folder” -ItemType Directory

Upon executing the command you should see a new folder in your current directory.

But what’s exactly happening here? Lets break it down:

  • The -Name parameter specifies a name for our item, replace “My Folder” with the name you would like your folder to have.
  • The -ItemType parameter specifies the type of item we want to create, in this case a folder. This is necessary because the new-item cmdlet can be used to create many types of item.
 

Different directories

The new-item cmdlet, by default, always creates items in your current directory, if you want to create your item in a different directory, you need to use the -Path parameter and specify that directory.

To minimize typing you can also include the name of your item within the path itself, without using the -name parameter at all. So to create a folder within the “My Folder” directory we created previously, first remove the name parameter, and then, enter a backwards slash at the end of your path followed by the desired name for your new directory.

New-Item -Path “My Folder\Folder” -ItemType Directory

A new folder named “Folder” should be created at once within the “My Folder” directory.

Creating Multiple types of files (Text, Scripts, Shortcuts).

Apart from creating folders, we can create various types of files using the new-item cmdlet.

Doing so is quite similar to creating directories, this time however using the -ItemType parameter isn’t necessary because the new-item cmdlet creates files by default.

Text Files.

Lets start simply by creating a text file in our current directory. Here is what that would look like:

New-Item -Name “TestFile.txt”

Pretty simple right?

Scripts:

Similarly you can create other types of files as well simply by changing the extension at the end of the name of your file.

Lets create a PowerShell script for example. PowerShell scripts use the .ps1 extension. So all we have to do is change the value of the name parameter from the example above. Here is what that would look like.

New-Item -Name “My Script.ps1”

Awesome! You now have a new script in your current directory. Similarly, you can create other types of scripts as well such as batch (.bat) or python (.py) simply by using the appropriate extension.

Easy as pie!

Shortcuts (Symbolic links):

Lets try something a little more complicated now, such as creating shortcuts.

Shortcuts cannot be created directly from the PowerShell console, not easily at least, however we can create something called a symbolic link which is essentially the same thing.

Without further a do, here is the command we are going to use:

New-Item -Name “My Shortcut” -Path C:\Users\Julian\Desktop -Target C:\Users\<Username>\Documents\File.txt -ItemType SymbolicLink

This might look a bit complicated, so lets break it down:

  • Just like before -Name specifies the name of our shortcut in this case it will be “My Shortcut”
  • -Path specifies the location in which we want our shortcut to be created at. In case you want to create your shortcut to be created in your current directory you don’t have to use this parameter at all.
  • -Target specifies the location of our original file. In this example a file named File.txt that is located in out documents directory. Replace <Username> with your computers username to make this command work for you.
  • Finally the -ItemType parameter lets the New-Item type cmdlet know that we want to create a symbolic link  (Shortcut).

That wasn’t hard at all!

Overwriting an existing file:

Previously we use the Test-Path cmdlet to check whether a file already exists before we create it. In some cases however you might want to purposefully overwrite an existing file.

Doing so its quite easy all we have to do is include the -Force parameter in our command as well. Here is what that would look like:

New-Item -Name “TestFile.txt” -Force

Our text file should now be overwritten. Awesome!

Different Directories:

Just like before, we can create any type of file in a directory other than our current one simply by using the -path parameter to specify the target directory.

To minimize typing, once again, I will include the name and extension of our file into our path. In this case the -name parameter is not necessary.

New-Item -Path “C:\Users\<Username>\Desktop\TestFile.txt”

The above command will create a file named “Testfile.txt” in our desktop directory.

To make it work for you, replace <Username> with your computer’s username.

Examples:

Creates a folder named “My Folder” in the current directory.

New-Item -Name “My Folder” -ItemType Directory

Creates a folder named “Folder” in the desktop directory. Replace <Username> with your computers username.

New-Item -Path “C:\Users\<Username>\Desktop\Folder” -ItemType Directory

Creates a script named “My Script” in the current directory.

New-Item -Name “My Script.ps1”

Creates a word document in your current directory, while overwriting any existing file.

New-Item -Name “My File.docx” -Force

Creates a text file named “TestFile” in the desktop directory. Replace <Username> with your computers username.

New-Item -Path “C:\Users\<Username>\Desktop\TestFile.txt”

Creates a shortcut named “My Shortcut” of a file named “File.txt” in the documents directory. Replace <Username> with your computers username.

New-Item -Name “My Shortcut” -Path C:\Users\Julian\Desktop -Target C:\Users\<Username>\Documents\File.txt -ItemType SymbolicLink

Summary:

  • We can perform any operation on an item that’s located in a different directory by entering its full path instead of directly its name.
  • Make sure you test your target path using the test-path cmdlet before creating a file, especially if you are using the -force parameter, so that you don’t accidentally overwrite any of your files.
  • When creating files, we can include the names of the items we want to create at the end of a path with the use of a backwards slash.

That's It!

You now know how to create files and folders, scripts and shortcuts directly from the PowerShell console.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows PowerShell.

Dont Stop Learning!

Windows PowerShell Course!

Learn the ins and outs of Windows PowerShell along with PowerShell scripting and become an expert!

Any Questions? Ask them Below:

Leave a Reply

Your email address will not be published.

Take Your Skills to the next level!
Become one of our beloved students by enrolling into one of our courses. Get started now!

Welcome

You are now officially on your way to becoming a genius!