How to Check if a Folder/Directory Exists With PowerShell

Use the New-Item cmdlet to create a new directory in PowerShell

Reading time icon 3 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help Windows Report sustain the editorial team Read more

Key notes

  • PowerShell is a command-line shell and scripting language developed by Microsoft.
  • It allows users to navigate, create, delete, and manipulate directories using various commands.
  • You can check if a directory exists with PowerShell using the Test-Pathcmdlet and create a new directory using the New-Item.
Fix powershell check if directory exists

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.

  • Directories are essential for organizing and managing files and data on your computer.
  • You can navigate, create, delete, and manipulate directories in PowerShell.
  • Further, some of the various commands, like cd (Change-Location), mkdir (New-Item), rmdir (Remove-Item), and others, are used to tweak directories.
  • Your user folder is your home directory, and the desktop is a subdirectory within your user folder. In other words, the desktop is located within your user directory.

How do I test if a directory exists in PowerShell?

Use the Test-Path cmdlet

  1. Left-click the Start button, type PowerShell, and click Run as administrator.
  2. Click Yes on the User Account Control prompt.
  3. Copy and paste the following command lines and press Enter:

$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. The Test-Path cmdlet with the parameter -PathType Container checks 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?

Use the New-Item cmdlet with the -ItemType parameter set to Directory

  1. Left-click the Start button, type PowerShell, and click Run as administrator.
  2. Click Yes on the User Account Control prompt.
  3. Copy and paste the following command lines and press Enter:

$directoryPath = "C:\path\to\your\directory"
if (-Not (Test-Path $directoryPath -PathType Container)) {
New-Item -ItemType Directory -Path $directoryPath | Out-Null
Write-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 print The directory that has been created or The output already exists.

In conclusion, you may be interested in our guide on how to copy files to Remote Computers with PowerShell on Windows 11. Also, we have a detailed guide about PowerShell not showing the full output and 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