Share this article
Improve this guide
How to Check if a Folder/Directory Exists With PowerShell
Use the New-Item cmdlet to create a new directory in PowerShell
3 min. read
Updated onOctober 4, 2023
updated onOctober 4, 2023
Share this article
Improve this guide
Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more
Key notes
PowerShell is an incredibly powerful tool, but many are wondering how to check if a directory exists in PowerShell.
This is pretty simple to do, and this article will discuss verifying if a directory exists on your system and the steps for creating new folders.
What are directories in PowerShell?
In PowerShell, directories are folders that can store files and other directories. They are similar to directories in other operating systems, such as Windows folders or Unix-based systems directories.
How do I test if a directory exists in PowerShell?
Use the Test-Path cmdlet
$directoryPath = “C:\path\to\your\directory"if (Test-Path $directoryPath -PathType Container) {Write-Host “The directory exists."} else {Write-Host “The directory does not exist."}
Replace the dummy path in our script with the actual path of the directory you want to check. TheTest-Path cmdletwith theparameter -PathType Containerchecks if the given path points to an existing directory (folder).
If the directory exists, it will print The directory exists. Otherwise, it will print The directory does not exist. As you can see, it’s pretty simple to check if a directory exists in PowerShell.
How can I create a directory with PowerShell?
$directoryPath = “C:\path\to\your\directory"if (-Not (Test-Path $directoryPath -PathType Container)) {New-Item -ItemType Directory -Path $directoryPath | Out-NullWrite-Host “The directory has been created."} else {Write-Host “The directory already exists."}
Replace the dummy path with the path where you want the new directory to be created. In the above command, the Test-Path cmdlet with the parameter -PathType Container checks if the directory already exists.
In cases where the directory does not exist, the New-Item cmdlet with -ItemType Directory creates it. The Out-Null part is used to suppress the output effect to the console when the directory is created.
However, depending on the situation, the script will printThe directory that has been createdorThe output already exists.
In conclusion, you may be interested in our guide on how tocopy files to Remote Computers with PowerShellon Windows 11. Also, we have a detailed guide aboutPowerShell not showing the full outputand some fixes to get it working on Windows 11.
Should you have further questions or suggestions, kindly drop them in the comments section.
More about the topics:PowerShell
Henderson Jayden Harper
Windows Software Expert
Passionate about technology, Crypto, software, Windows, and everything computer-related, he spends most of his time developing new skills and learning more about the tech world.
He also enjoys gaming, writing, walking his dog, and reading and learning about new cultures. He also enjoys spending private time connecting with nature.
User forum
0 messages
Sort by:LatestOldestMost Votes
Comment*
Name*
Email*
Commenting as.Not you?
Save information for future comments
Comment
Δ
Henderson Jayden Harper
Windows Software Expert
Passionate about technology, Crypto, software, Windows, and everything computer-related, he spends most of his time developing new skills.