How to Create Directory in PowerShell: Simple Guide
To create a directory in PowerShell, use the
New-Item cmdlet with the -ItemType Directory parameter. For example, New-Item -Path 'C:\ExampleFolder' -ItemType Directory creates a new folder named 'ExampleFolder'.Syntax
The basic syntax to create a directory in PowerShell uses the New-Item cmdlet with these parts:
-Path: Specifies the folder path you want to create.-ItemType Directory: Tells PowerShell to create a folder (directory).
powershell
New-Item -Path 'C:\FolderName' -ItemType DirectoryExample
This example creates a new folder named MyNewFolder on the C drive. It shows how to run the command and what output to expect.
powershell
New-Item -Path 'C:\MyNewFolder' -ItemType DirectoryOutput
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 4/27/2024 10:00 AM MyNewFolder
Common Pitfalls
Common mistakes when creating directories in PowerShell include:
- Not specifying
-ItemType Directory, which creates a file instead of a folder. - Using a path that already exists, which causes an error.
- Not having permission to create folders in the target location.
Always check if the folder exists before creating it to avoid errors.
powershell
if (-Not (Test-Path -Path 'C:\MyNewFolder')) { New-Item -Path 'C:\MyNewFolder' -ItemType Directory } else { Write-Output 'Folder already exists.' }
Output
Folder already exists.
Quick Reference
| Command | Description |
|---|---|
| New-Item -Path 'C:\Folder' -ItemType Directory | Creates a new folder at the specified path |
| Test-Path -Path 'C:\Folder' | Checks if the folder exists |
| Remove-Item -Path 'C:\Folder' -Recurse | Deletes the folder and its contents |
Key Takeaways
Use New-Item with -ItemType Directory to create folders in PowerShell.
Always check if the folder exists with Test-Path before creating it.
Specify the full path to avoid creating folders in unexpected locations.
Lack of permissions can prevent folder creation; run PowerShell as admin if needed.