Powershell

A Better Way to Check if a Path Exists or not in PowerShell

PowerShell is the Windows tool that lets you perform several File Explorer operations, including copying, renaming, moving, deleting, or creating new files or folders. However, while performing some operations in File Explorer, we need to check if a certain path exists or not. Although this task can be performed using File Explorer, PowerShell also offers you to check if the specified folder/directory exists or not as a command-line method.

This post will illustrate a procedure to examine whether the path exists in PowerShell.

What is a Better Way in PowerShell to Check/Examine if a Path/Directory Exists or not?

These are the approaches that can be used to check whether a certain path exists or not:

Method 1: Check if a Path Exists or not in PowerShell Using “Test-Path” Command

Test-Path” is a PowerShell command specially used to check if the provided path exists or not. At first, it checks the specified path in Windows. If it exists, the command will return “True”, but if the path does not exist, “False” will be returned.

Now, let’s check some demonstrations better to understand the working of the “Test-Path” command.

Example: Use the Test-Path Command to Check the Path if it Exists or not

In this example, we will test if the specified path in Windows if it exists or not in PowerShell:

> Test-Path "C:\Program Files"

The command has returned “True”, meaning the path/directory exists.

Now, let’s test a non-existent path using the same command:

> Test-Path "C:\Program Files\New"

It can be observed that the “False” boolean indicates that the path does not exist.

Method 2: Check if a Path Exists or not in PowerShell Using [System.IO.Directory]::Exists() Method

System.IO.Directory” is a .NET class that is utilized to perform several directory operations, such as creating, removing, renaming, copying, or moving the directories. More specifically, when the “Exists()” method is used with the System.IO.Directory class, it will check for the provided path in Windows. If it exists, the boolean value “True” will be returned, else “False”.

Example

This example will check the file path using the “Exists()” method as follows:

> [System.IO.Directory]::Exists("C:\Windows")

The output is returned as “True”, which means that the provided path exists in the system.

Now, let’s utilize the “[System.IO.Directory]::Exists()” method to verify if the specified path does not exist:

> [System.IO.Directory]::Exists("C:\Windows\New")

That was all about checking if a path exists or not in PowerShell.

Conclusion

The path in the PowerShell can be checked if it exists or not by using several methods. These methods include the “Test-Path” command and the “[System.IO.Directory]::Exists()” method. Both approaches first take the path, verify it in the system, and then output a boolean value according to the given results. This tutorial has presented a guide to check whether a certain path exists in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.