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.

Getting started with folder creation in PowerShell

Creating directories is a fundamental task in any scripting or programming language. The New-Item cmdlet is a versatile command in PowerShell that can be used to create various types of items, including directories. 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 parent folder 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. The double quotes wrap the parameters, such as Path, so that you can use special characters like Space in it.

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"

In this example, the parent folders will be created automatically.

Checking if a folder exists in the PowerShell

Before creating a new folder or directory, it’s important to check if it already exists. PowerShell provides several ways to accomplish this. One approach uses the Test-Path cmdlet, which checks if a given path exists. Here’s an example:

if (Test-Path -Path C:\Path\To\Folder) {
    Write-Host "Folder already exists."
} else {
    Write-Host "Folder does not exist."
}

In this example, we use the Test-Path cmdlet to check if the folder at the specified path exists. If the folder exists, a message is displayed indicating its existence. Otherwise, a message is displayed indicating that the folder does not exist.

PowerShell to create a directory if not exists

You can also use the Get-ChildItem cmdlet to test if a folder exists in a given path:

$FolderPath = "C:\Temp\Reports"

If(Get-ChildItem -Path $FolderPath -Force -ErrorAction SilentlyContinue)
{
    Write-Host "Folder already Exists!"
}
else
{
    #PowerShell Create directory if not exists
    New-Item $FolderPath -ItemType Directory
    
    Write-Host "New Folder Created!"
}

The above script also creates a new folder using the PowerShell New-Item cmdlet.

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, you can use the PowerShell Test-Path cmdlet. Let’s alter the script a bit to first 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 command 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

This allows you to automate the process and create multiple folders based on specific criteria or a predefined list. 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.

We can use a loop to iterate through a list of directory names. For example, to create directories named “New Folder 1”, “New Folder 2”, “New Folder 3”, and so on in the “C:\Temp” folder, we would use the following command:

1..10 | ForEach-Object { New-Item -ItemType Directory -Path "C:\Temp\New Folder $_"}

Bulk Create Folders from a Text File

To create multiple folders, you can start by preparing a list of folder names (Just Folder names like “Folder 1”, “Folder 2”, etc.) in a text file, such as “FoldersToCreate.txt”. Each folder name should be on a separate line within the text file. Next, you can use a PowerShell script to read the list of folder names from the text file and create the corresponding folders. Here’s an example script:

$FolderNames = Get-Content -Path 'C:\Temp\FoldersToCreate.txt'
$DirectoryPath = "C:\Temp"

ForEach ($FolderName in $FolderNames) {
    New-Item -ItemType Directory -Path (Join-Path -Path $DirectoryPath -ChildPath $FolderName)
}

In this script, the Get-Content cmdlet is used to read the contents of the “FoldersToCreate.txt” file. The $directoryPath variable represents the path where you want to create the folders. The foreach loop iterates through each folder name in the list and uses the New-Item cmdlet to create the corresponding folders. By running this script in the PowerShell console, you can create multiple folders based on the names specified in the text file.

Similarly, you can use CSV files to buk create Folders. Here is my sample CSV:

PowerShell script to Create Multiple Folders from CSV

The following script imports the CSV file folderList.csv, then loops through each row in the CSV. For each row, it creates a new directory with the path specified in the FolderPath column. The -Force flag allows the command to create any necessary parent directories, and also avoids throwing an error if the directory already exists.

#Get Data from CSV
$FolderList = Import-Csv -Path "C:\Temp\FolderList.csv"

#Iterate through the CSV
ForEach ($Row in $FolderList) {
    #Create Each Folder
    New-Item -ItemType Directory -Force -Path $Row.FolderPath
}
Bulk create folders powershell

Make sure you are running the script as Administrator; otherwise, you’ll face an “Access Denied” Error.

Create a Folder with the Current Date-Timestamp

You may want to create unique folders with the date timestamps for various purposes like backup. Here is the PowerShell script that will create a folder with the current date timestamp in the format: 19-Sep-2022 06-57-00-PM (You can use a different format, if needed!)

# Get the Current Date Formatted
$DateString = (Get-Date).ToString('dd-MMM-yyyy hh-mm-ss-tt')

# Frame the Folder Name
$FolderPath = "C:\Backup\$DateString"

# Check Folder Exists
If (Test-Path -Path $FolderPath) {
    #Get the Folder object
    $Folder = Get-Item -Path $FolderPath
    Write-Host "Folder already exists." -f Yellow
} Else {
    #Create a New Folder with Date Timestamp    
    $Folder = New-Item -ItemType Directory -Path $FolderPath
    Write-Host "Folder Created Successfully!." -f Green
}

When you run this script, it will create a new folder in the specified directory with the current timestamp. If the folder already exists, then the script gets that folder.

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

The File System Object and the .Net System.IO.Directory Methods

The FileSystemObject (FSO) allows you to work with files and directories in PowerShell. Here is how to create a directory in PowerShell:

$NewDir = "C:\Temp\John"
$FSO = New-Object -ComObject scripting.filesystemobject
$FSO.CreateFolder($NewDir)

This will create the directory C:\Temp\John, if it doesn’t already exist. Similarly, using System.IO method:

$NewDir = "C:\Temp\John"
[System.IO.Directory]::CreateDirectory($newDir)

Best practices for Creating and managing folders in PowerShell

When working with folders and directories in PowerShell, it’s important to follow best practices for organization and management. Here are some tips to consider:

  1. Use meaningful names: Choose descriptive names for your folders and directories that accurately reflect their purpose or contents. This will make it easier to navigate and understand the file structure.
  2. Implement a consistent naming convention: Establish a consistent naming convention for your folders and directories. This will help maintain a logical and organized file structure, especially when multiple people are involved.
  3. Plan for scalability: Anticipate future growth and consider the scalability of your folder and directory structure. Design your file organization in a way that can accommodate future additions and changes without causing confusion or inefficiency.
  4. Document your structure: Maintain documentation that outlines the structure and purpose of your folders and directories. This will help other team members understand and navigate the file system.
  5. Regularly review and clean up: Periodically review your folder and directory structure to identify obsolete or unused items. Remove them to keep your file system clean and efficient.
  6. Special Characters in Folder Names: When creating folders in PowerShell, it is important to handle special characters in folder names properly. Special characters such as spaces, dots, or non-alphanumeric characters can sometimes cause issues when working with folders.
  7. Handle errors and exceptions: Implement error handling mechanisms in your scripts to gracefully handle any errors or exceptions that may occur during folder creation. This ensures a smooth execution process and provides feedback on any issues encountered.
  8. Check for directory existence: Before creating a new directory, check if it already exists using the Test-Path cmdlet. This prevents errors and avoids overwriting existing directories.
  9. Check Permissions: Ensure that the user running the PowerShell script has the necessary permissions to create directories in the specified location.
  10. Invalid characters: Avoid using invalid characters in directory names, such as slashes (/ or ), colons (:), question marks (?), and asterisks (*). These characters are reserved for specific purposes and can cause issues when creating directories.

By following these best practices, you can ensure a well-organized and manageable file structure in PowerShell.

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!

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.

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 PowerShell equivalent of mkdir?

The PowerShell equivalent of the mkdir command is the New-Item cmdlet. Here’s an example of how to use New-Item to create a directory:
New-Item -ItemType Directory -Path "C:\Path\to\directory"

How to Create a Hidden Folder using PowerShell?

You can create a new folder and set its Attributes to “Hidden”. Here is how:
$Path = "C:\temp\Hidden Folder"
#Create a New Folder in PowerShell
New-Item -Path $Path -ItemType Directory
#Hide the Folder
Set-ItemProperty -Path $Path -Name attributes -Value ([io.fileattributes]::Hidden)

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “How to Create a Folder using PowerShell?

Leave a Reply

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