Powershell

Create a Folder in PowerShell

The organization of data and files is an important activity to consider in computing. The more organized your files are, the more confident you will feel while finding them. For example, if you are working on a project and throwing the files on different drives in the computer, it will be challenging to look for the files after a few months. On the other hand, if you keep your files on a single drive and inside a single folder, it will be easier to locate them whenever the files are needed.

There are multiple ways to create directories on windows. Windows Command Prompt (CMD) can create directories/folders by manually accessing the targeted location. However, we are using PowerShell in this guide. Therefore, before starting, we will enumerate several fundamental differences between CMD and PowerShell.

Differences Between PowerShell (PS) and CMD

For every task that you can perform in CMD, those tasks can also be performed in PowerShell. However, the following are the differences between PowerShell and CMD:

  • PowerShell primarily relies on cmdlets and batch commands. Moreover, PowerShell returns the output as an object. On the other hand, CMD uses batch commands in text form.
  • PowerShell supports Microsoft Windows and Linux-based distributions; however, CMD does not provide support for Linux.
  • PowerShell can execute multiple cmdlets commands put together in a sequence, while CMD executes the commands one by one.

How to Create Folders in Windows Using PowerShell

There are multiple ways you can make directories in PowerShell, and we will discuss them in detail.

First, open the PowerShell terminal to proceed further. To do this, click on the “Search” on the taskbar and type “PowerShell” there. Click on “PowerShell” to open it:

Method 1:

The first way is straightforward, and if you know the Windows Command Prompt (CMD), you will be familiar with this command. In both shells, the command works the same. The command given below will create “directory1” in the present working directory:

> mkdir directory1

However, if you want to create a directory in any other drive or location, you must specify the path. For example, the following command will create “directory1” in the “E” drive:

> mkdir E:\directory1

Method 2:

The other way to make directories using PowerShell is also the same as we do in CMD. You can run the command given below to make directories:

> md directory2

Moreover, if you create the directory in other drives, you must guide the path where you want to get the new directory:

> md E:\directory2

Method 3:

The following method creates the directory using the “new-item” command, as shown below: the command will create “directory3” in your present working directory:

> new-item directory3 -itemtype directory

Moreover, if you want to create the directory at any defined location, you will have to specify the path before the directory name:

> new-item E:\directory3 -itemtype directory

Method 4:

This method will also create directories at your desired location using the scripting-filesystemobject object. This object contains files and directories for various operating systems, such as UNIX and all OS of windows. VBScript also uses the same thing for file handling. Moreover, this method will first generate the scripting object. After, the “CreateFolder” will enable you to make a new directory.

If you want to create the folder in the present working directory, then copy and paste the following command to create “directory4” in the present working directory:

> $fso = new-object -ComObject scripting.filesystemobject
> $fso.CreateFolder(“directory4”)

The output of this command contains rich information related to the new directory, such as “ShortPath”, “ShortName,” and “Type”. These distinct fields are not present in any other output of the command.

Moreover, if you require a directory at some other location in your PC, then execute the command given below to do it:

> $fso = new-object -ComObject scripting.filesystemobject
> $fso.CreateFolder("E:\directory4")

Method 5:

You can use the .NET Framework Class to create a directory using the system.io.directory. First, this method will fetch the directory class of the .NET Framework. That directory class will be used to create a new directory in your system to create a new directory in your present working directory. Use the command mentioned below:

>[system.io.directory]::CreateDirectory(“directory5”)

However, to create a directory somewhere else on PC, you must specify the path of the said location inside double-quotes. The following command will create a directory in our “E” drive:

>[system.io.directory]::CreateDirectory(“E:\directory5”)

You will notice here a different output. The output will not show you the path where the directory is created. However, if you have written the correct command, the guide will be made at the path given in the command.

Conclusion

It really matters how you organize your “data” in computing. One can see the importance of how you manage your data files and store them for future use. For this, folders or directories help to manage your files or data. This guide shows various methods to create folders using PowerShell. However, the “mkdir” and “md” commands are the easiest way to create directories. Moreover, it depends on the users which method they want to implement.

About the author

Adnan Shabbir