In this tutorial, you will learn how to create directories using PowerShell. PowerShell is a versatile scripting language developed by Microsoft, designed for system administrators to automate tasks such as creating directories, managing files, and executing processes.
By the end of this tutorial, you will have learned how to create a new directory, specify a path, and create multiple directories in a single command.
Step 1: Open PowerShell
First, open the PowerShell console by typing “PowerShell” in the Windows search bar and selecting the “Windows PowerShell” application. You may want to open it as an administrator if you plan on creating directories in protected locations.
Step 2: Use the New-Item Command
The New-Item
command is used to create a new directory. The syntax for using the New-Item
command to create a directory is as follows:
1 |
New-Item -Path -ItemType Directory |
Replace <Path>
with the filepath of the directory you want to create. For example, if you want to create a new directory called “MyFolder” within your “Documents” directory, the command would look like:
powershell
New-Item -Path C:\Users\YourUsername\Documents\MyFolder -ItemType Directory
After executing the command, you should see an output indicating that the new directory has been created successfully:
Directory: C:\Users\YourUsername\Documents Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/12/2021 2:30 PM MyFolder
Step 3: Create Nested Directories
If you need to create multiple nested directories, you can do so by specifying the entire path and using the -Force
parameter. The -Force
parameter ensures that all missing directories in the specified path will be created as well.
For example, to create a series of directories in the following structure “ParentFolder\ChildFolder\SubChildFolder”, use the following command:
1 |
New-Item -Path C:\Users\YourUsername\Documents\ParentFolder\ChildFolder\SubChildFolder -ItemType Directory -Force |
Step 4: Create Multiple Directories Simultaneously
You can create multiple directories at once by using an array to define the directory paths. In this example, three directories named “FolderA”, “FolderB”, and “FolderC” will be created under the “Documents” directory:
1 2 |
$folders = @("C:\Users\YourUsername\Documents\FolderA", "C:\Users\YourUsername\Documents\FolderB", "C:\Users\YourUsername\Documents\FolderC") ForEach ($folder in $folders) {New-Item -Path $folder -ItemType Directory} |
Full Code
1 2 3 4 5 6 |
New-Item -Path C:\Users\YourUsername\Documents\MyFolder -ItemType Directory New-Item -Path C:\Users\YourUsername\Documents\ParentFolder\ChildFolder\SubChildFolder -ItemType Directory -Force $folders = @("C:\Users\YourUsername\Documents\FolderA", "C:\Users\YourUsername\Documents\FolderB", "C:\Users\YourUsername\Documents\FolderC") ForEach ($folder in $folders) {New-Item -Path $folder -ItemType Directory} |
Conclusion
In this tutorial, you learned how to create directories in PowerShell using the New-Item
command. You can now efficiently create single directories, nested directories, and multiple directories simultaneously.
PowerShell offers a powerful way to automate tasks, making it an invaluable tool for system administrators and developers alike.