| by Arround The Web | No comments

How to Create a Directory in PowerShell

Folders or directories are normally created using GUI (Graphical User Interface) in Windows. However, PowerShell can also be utilized for this cause. It offers some specific commands, such as “New-Item” or “mkdir” that are specially designed for creating a directory. Moreover, these commands also use additional parameters in order to overwrite an existing folder or directory.

In this article, the creation of the directory using will be demonstrated.

How to Create a Directory in PowerShell?

These approaches can be used to create a directory in PowerShell:

Method 1: Use the “New-Item” Cmdlet to Create a Directory

The cmdlet “New-Item” in PowerShell creates a new item such as a file or folder. It uses the “-ItemType” and the “-Path” parameters to create a directory with the desired name and location.

Example
This demonstration will create a new directory in PowerShell:

New-Item -ItemType Directory -Path C:\Doc\New_1 -Force

According to the above code:

  • First, add the “New-Item” cmdlet, then specify the “-ItemType” parameter and define the value “Directory” to it.
  • Then, specify the “-Path” parameter and define the path along with the file name to be created.
  • Lastly, add a “-Force” parameter in order to overwrite the existing folder:

Method 2: Use the File System Object to Create a Directory

The file system object is another method that can be used to create a new directory at a specified path or folder in PowerShell.

Example
This illustration will create a new directory in PowerShell:

[System.IO.Directory]::CreateDirectory("C:\Doc\New")

According to the above code:

  • First, add the “System.IO.Directory” .NET class to fetch the .NET class directory and then call the “CreateDirectory()” method to create a new directory.
  • Add the directory path followed by the name inside the “CreateDirectory()” method:

Method 3: Use the “mkdir” Cmdlet to Create a Directory in PowerShell

The “mkdir” cmdlet in PowerShell creates a directory in the current working directory. However, if the path is defined, it will create a directory in it. More specifically, it is an alias of the “New-Item” cmdlet.

Example
In this instance, a new directory will be created using the “mkdir” cmdlet:

mkdir C:\Doc\New_3

In accordance with the above code:

  • First, add the “mkdir” cmdlet and then specify the directory address along with the name of the address to be created:

Method 4: Use the “md” Cmdlet to Create a Directory in PowerShell

The “md” cmdlet can also be used to create a new directory in a specified path using PowerShell. It is an alias of the “mkdir” cmdlet.

Example
This illustration will create a directory in PowerShell using the “md” cmdlet:

md C:\Doc\New_2

According to the above code:

  • First, specify the “md” cmdlet and then add the folder along with its name to be created:

That was all about creating directories in PowerShell.

Conclusion

The directory in PowerShell can be created using several cmdlets. These cmdlets include “New-Item”, “mkdir”, or “md”. To create a directory, first, add the relevant cmdlet and then specify the directory along with the file name. This write-up has observed a guide to create a new directory in PowerShell.

Share Button

Source: linuxhint.com

Leave a Reply