How to Create a Folder using PowerShell?

Requirement: Create a new folder in PowerShell.

PowerShell to create a Folder

As an Administrator, I always look for ways to streamline my workflow and increase productivity. One tool that has been a game-changer for me is PowerShell. PowerShell is designed to automate tasks, so if you find yourself doing something repeatedly, chances are that you can automate it using PowerShell. With PowerShell, I can automate repetitive tasks and easily perform complex operations, saving me a lot of time and effort. One task that I frequently perform is creating folders.

If you are looking for an easy way to create a directory in PowerShell, this how-to guide is for you. With PowerShell, you can quickly create new folders without using the GUI. This post will show you how to create a folder using PowerShell step-by-step.

Benefits of Using PowerShell to Create Folders

There are several benefits to using PowerShell to create folders:

  1. Speed: PowerShell is much faster than creating folders manually. You can create multiple folders in a matter of seconds.
  2. Consistency: When creating folders manually, there is always the chance of making a mistake or forgetting to follow a naming convention. With PowerShell, you can ensure that all folders are created with the correct name and in the correct location.
  3. Automation: If you need to create folders on a regular basis, PowerShell allows you to automate the process, saving you time and effort.

To create a new folder in PowerShell, you can use the New-Item command with the -ItemType parameter set to Directory. For example: Let’s create a new folder, “Temp”, in the path “C:\”.

To create a new folder using PowerShell, simply open PowerShell and type in the following command:

New-Item -Path "C:\" -Name "Temp" -ItemType Directory

This cmdlet takes parameters such as “-Path” for the parent directory, “-Name” for the name of the Folder, “-ItemType” for the File or Directory, and so on. The force parameter doesn’t overwrite the folder, but gets the existing folder object with its contents intact.

This will create a new folder with the specified name “Temp” in the specified location “C:\”, If the C:\Temp directory doesn’t exist. The following command creates a directory named “Temp” in the current directory.

New-Item -Path .\Temp -ItemType Directory

Or use the below command to create a new directory under the current directory:

New-Item "Temp" -ItemType Directory

The New-Item cmdlet can also create a new registry key and file system objects like files and folders.

Using PowerShell to Create Nested Folders and Subdirectories

Creating folders is a task that we often perform when working with files. Folders are used to organize files and keep them in a logical structure. We can manually create folders by right-clicking in Windows Explorer and selecting “New Folder.” However, if we need to create multiple folders or nested folders, this can be a time-consuming task. This is where PowerShell comes in handy.

New-Item -ItemType Directory -Path "C:\Temp\Documents\New Folder\Subfolder1\Subfolder2"

PowerShell to Create a Folder if it does not exist

What if a folder “Temp” exists already in the “C:\”? Before creating a folder, you may want to check if it already exists. To check if a folder or directory exists in PowerShell, you can use the Test-Path cmdlet. Let’s alter the script a bit to check if the folder not exists and then create the folder.

#Variable
$FolderPath= "C:\Temp"

#Check if Folder exists
If(!(Test-Path -Path $FolderPath))
{
    #powershell create directory
    New-Item -ItemType Directory -Path $FolderPath
    Write-Host "New folder created successfully!" -f Green
}
Else
{
  Write-Host "Folder already exists!" -f Yellow
}

You can also use the md alias for the New-Item cmdlet to create a make a new directory in PowerShell:

md C:\Example\NewFolder

Note that you will need to have the appropriate permissions to create a new folder in the specified location.

Creating Subfolders using PowerShell

You can also use this same process to create subfolders within existing folders. To do this, simply enter the command in the following syntax: “New-Item -Path < sub-folder Path> -Type Directory”, and press Enter. E.g.

New-item -Path "C:\New Folder\New" -ItemType Directory

This creates the entire Folder-tree in the given path.

Creating Multiple Folders using PowerShell

To create multiple folders and directories at the same time, you can use the New-Item cmdlet with the -ItemType parameter set to Directory. For example, the following command creates three directories named Temp1, Temp2, and Temp3 in the C:\ drive.

New-Item -Path C:\Temp1,C:\Temp2,C:\Temp3 -ItemType Directory

Similarly, you can create multiple folders with For Loop as:

$Folders = @("Folder1", "Folder2", "Folder3")

ForEach ($Folder in $Folders) {
    New-Item -ItemType Directory -Path "C:\Temp\Documents\$Folder"
}

In this example, we are creating three folders named “Folder1,” “Folder2,” and “Folder3” in the “Documents” folder of the “C:\Temp” path. The $Folders variable contains an array of folder names, and the ForEach loop iterates over each folder name and creates a new folder using the New-Item cmdlet.

PowerShell to Create Folders in all Sub-Folders

To create folders in all Sub-folders at the given Folder (1st level sub-folders), use:

New-item .\Temp\* -Name "New Folder" -ItemType Directory

Wrapping up

In this article, we have looked at how to create folders like a pro using PowerShell. We have covered the basic syntax for creating folders, creating multiple folders at once, using PowerShell to create nested folders and subdirectories, and checking if a folder or directory exists before creating it.

Creating folders with PowerShell is an easy and efficient way to organize your computer system. With just a few simple steps, you can quickly create folders in any location on your computer. So next time you need to quickly organize your files into folders, try using PowerShell!

How to create a file in PowerShell?

To create a new file in PowerShell, you can use the New-Item cmdlet. For example, to create a new text file named “example.txt” in the current directory, you can use the following command: New-Item -ItemType File -Name “example.txt”. You can also specify the path where you want to create the file by adding the -Path parameter. Use the -Force switch to overwrite existing files.

What is the difference between a folder and a subfolder?

A folder is a container that holds files or other folders, while a subfolder is a folder that is contained within another folder. In other words, a subfolder is a folder that is nested inside another folder.

What is the command to create a new folder in PowerShell?

The command to create a new folder in PowerShell is “New-Item -ItemType Directory -Path C:\Path\To\New\Folder”. Simply replace “Path\To\New\Folder” with the desired path and folder name.

Is there a way to make multiple folders at once?

Yes, it is possible to create multiple folders at once using PowerShell. Simply open the PowerShell command line and use the New-Item cmdlet with the -ItemType Directory parameter to create the folders. You can also use variables and loops to create a large number of folders with specific names or properties, as explained in this article.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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