Table of Contents
Using Built-in Cmdlets and Methods
We can use built-in cmdlets and methods to check if a directory exists.
Use the Test-Path
Cmdlet
To check if a folder exists, use the Test-Path
cmdlet.
1 2 3 4 5 6 7 8 |
$path = "C:\Windows" if(test-Path -Path $path){ Write-Host "Folder exists." }else{ Write-Host "Folder does not exist." } |
1 2 3 |
Folder exists. |
PowerShell’s Test-Path cmdlet verifies that a path exists, regardless of whether it is a file or folder. This cmdlet helps write scripts that need verification that specific files and folders exist before continuing with the rest of the script. If the path
exists, it returns true
, else false
.
We can also use the Test-Path cmdlet to check for specific permissions on files or folders and the existence of environment variables. For example, we used the Test-Path
cmdlet to check if the folder exists. We enclosed the test-path
within if
statement; if the path
exists, it prints Folder exists.
, else Folder does not exists.
.
Use the [System.IO.Directory]::Exists()
Method
To assure that a directory exists, use the [System.IO.Directory]::Exists()
method.
1 2 3 4 5 6 7 8 |
$path = "C:\Windows" if([System.IO.Directory]::Exists($path)){ Write-Host "Folder exists." }else{ Write-Host "Folder does not exist." } |
1 2 3 |
Folder exists. |
[System.IO.Directory] is a PowerShell module that provides a set of cmdlets for working with directories and files. It allows us to create, delete, copy, move and rename directories and files. It also provides cmdlets for listing the contents of directories and setting permissions on directories and files.
The [System.IO.Directory]
module is included in Windows PowerShell 5.0 or later and is available on the PowerShell Gallery. With this module, we can easily manage files and directories from the command line or script them for automated tasks.
Sometimes we may need to check if a folder or directory exists. In PowerShell, we can use the [System.IO.Directory]::Exists() method from the .NET Framework. To use it, pass the path to the folder or directory we want to check. If the folder or directory exists, it will return True
, else False
. For example, we used this method to check whether the path
exists.
Further, we wrapped the Exists()
method with if
statement to display Folder exists.
message if the if
condition was fulfilled; otherwise, else
block would be executed.
Use the Get-Item
Cmdlet
To verify if a folder exists, use the Get-Item
cmdlet.
1 2 3 4 |
$path = "C:\Windows" Get-Item -Path $path -ErrorAction SilentlyContinue |
1 2 3 4 5 6 |
Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/25/2022 2:50 PM Windows |
The Get-Item cmdlet retrieves items, such as files, folders, registry keys, and other objects from a given location in PowerShell. In addition, Get-Item
can accept input from the pipeline or get items by providing a path. Finally, the cmdlet displays the information about the directory or a file if it exists.
It has many parameters available that make it powerful such as the -ErrorAction
with the SilentlyContinue
value suppresses all the errors and continues the script. For example, we used the cmdlet with the -ErrorAction SilentlyContinue
parameter to check if the path
exists. If it is, it will display the information regarding the file on the console, else nothing.
We can use a variable to capture the result and check if it has any value to determine whether the folder exists.
1 2 3 4 5 6 7 8 9 |
$path = "C:\Windows" $folder = Get-Item -Path $path -ErrorAction SilentlyContinue if ($folder){ Write-Host "Folder exists." }else{ Write-Host "Folder does not exist." } |
1 2 3 |
Folder exists. |
If the folder exists, the value of the folder
variable will be the folder object, and the code inside the if
block executes and prints Folder exists.
using Write-Host
cmdlet. If the folder does not exist, the value of the folder
variable will be null
, and the code inside the else
block would be executed.
Further reading:
Other Cmdlets
We can replace the Get-Item
cmdlet with the below-given cmdlets.
- The Get-ChildItem cmdlet displays the contents of the
path
if it exists. - The
dir
is an alias of theGet-ChildItem
cmdlet. - The Get-ItemProperty cmdlet displays the information regarding the
path
if it exists.
Creating Custom Alias
To check if a directory exists, we can create custom aliases using New-Alias
.
1 2 3 4 5 6 7 8 9 |
$path = "C:\Windows" New-Alias exists Test-Path if(exists $path){ Write-Host "Folder exists." }else{ Write-Host "Folder does not exist." } |
1 2 3 |
Folder exists. |
The New-Alias cmdlet in PowerShell lets us create our aliases for cmdlets, functions, and scripts. Aliases are useful for creating shorter, easier-to-remember names for common commands. They are easy to set up and save time and effort when working with PowerShell. In addition, we can create multiple aliases for the same thing, each with a different name.
For example, we created the exists
alias for the Test-Path
cmdlet. Note that we can replace the Test-Path
cmdlet with other cmdlets we discussed in the code section for using built-in cmdlets and methods with their respective parameters.
Next, we enclosed the exists
alias within the if
statement to print Folder exists.
if the specified folder exists; otherwise, the above code will execute the else
block and display Folder does not exists.
.
That’s all about how to check if folder exists in PowerShell.