in

WindowsWindows Loving ItLoving It

New Directory and File in 1 step PowerShell using New-Item

Easily create a directory and file with just New-Item in PowerShell. Alias will decrease the command lenght.

PowerShell new item

In this post, we are going to see some of the most commonly used PowerShell command new-item.   It is used to create a new item depending on the context.

The new-item can create a file as well as a Registry key and more.  If the new item is used on a file system, it will create a new file or a folder and add some initial values to it and if it were used on a registry, it will create a new registry key and set its value too.

Jump to:

People also search:

Firstly let us see how to create a folder using PowerShell.  We will also see the many advantages we get by using this command.

Create a new folder using New-Item in PowerShell:

We can create a new folder named “my movies” in a directory using the following command.

new-item “my movies” -itemtype “directory”
new item directory
new item directory

Complete instructions on the above command are as follows:

  • Go to the location where you want to create a new folder.
new directory location
new directory location
  • Click on the address bar and type PowerShell.
location address bar
location address bar
PowerShell
PowerShell
  • This will launch PowerShell in that location.
PowerShell in the location
PowerShell in the location
  • Now enter the command
new-item “my movies” -itemtype “directory”
Create directory
Create directory

It creates the folder as shown below.

new folder
new folder

Create a new file using New-Item in PowerShell:

We can easily create a new file using PowerShell by specifying the type of item.  The below command does just that.

new-item “my file.txt” -itemtype “file”
PowerShell create file
PowerShell create file

new-item short form (alias) and how to use it:

The new-item has a short form (alias) and it is ni.  The -itemtype flag can also be replaced with -type flag.  We do not need double quotes around directory and file because they do not have white-spaces in them.

The command for creating directory will look like this now:

ni “my movies” -type directory
short hand for creating new directory
short hand for creating new directory

And the create file command will look like this:

ni “my file.txt” -type file
short hand for creating new file
short hand for creating new file

Verdict:

The new-item is used for creating new items depending on the context.  We can create a new directory or a file with the help of the -itemtype flag.

We can simplify the command with the help of alias.  The new-item is replaced with just ni and the -itemtype is replaced with -type.

If you want to know more about PowerShell, stay tuned to TecKangaroo.

Leave a Reply

Your email address will not be published. Required fields are marked *