https://www.delftstack.com/howto/powershell/powershell-check-if-folder-exists/
Check if Folder Exists in PowerShell
PowerShell is a powerful tool that can perform different files and folders operations. It allows you to create, copy, move, rename, delete, and view files and folders on the system.
This tutorial will introduce different methods to check if a folder exists on the system using PowerShell.
Use the Test-Path Cmdlet to Check if Folder Exists in PowerShell
The Test-Path cmdlet determines whether all path elements exist or not in PowerShell. It returns a Boolean value, True if all elements exist, and False if any are missing.
For example, the following command checks whether all elements of the path C:\New\complex exist or not.
Test-Path -Path "C:\New\complex"
Output:
True
It means the complex folder exists in the C:\New directory.
This command checks if the Documents folder exists in the C:\New directory.
Test-Path -Path "C:\New\Documents"
Output:
False
Hence, the Documents folder is not present in the C:\New directory.
If you want to return verbose information instead of True/False, you can use the if statement like this.
if (Test-Path -Path "C:\New\Documents"){
Write-Host "The given folder exists."
}
else {
Write-Host "The given folder does not exist."
}
Output:
The given folder does not exist.
Use System.IO.Directory to Check if Folder Exists in PowerShell
The System.IO.Directory class from the .NET
Framework provides static methods for creating, moving, deleting, and
enumerating through directories and subdirectories. You can use its Exists() method to determine whether the specified path refers to an existing directory on the system.
It also returns True if the path exists and False if it does not exist.
[System.IO.Directory]::Exists("C:\New\complex")
Output:
True
Now, let’s check if the Documents folder exists in the C:\New directory.
[System.IO.Directory]::Exists("C:\New\Documents")
Output:
False
Use the Get-Item Cmdlet to Check if Folder Exists in PowerShell
The Get-Item cmdlet gets the item at the given path.
If the path exists on the system, it prints the Mode, LastWriteTime, Length, and Name of the directory.
Get-Item C:\New\complex
Output:
Directory: C:\New
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 1/11/2022 10:12 PM complex
If the specified path does not exist, you will get an error message saying it does not exist.
Get-Item C:\New\Documents
Output:
Get-Item : Cannot find path 'C:\New\Documents' because it does not exist.
At line:1 char:1
+ Get-Item C:\New\Documents
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\New\Documents:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
You can also use the above methods to check if a file exists on the system. We hope this article gave you an idea of checking if a folder exists in PowerShell.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.