PowerShell: Test-Path cmdlet

Test-Path allows us to verify if an item exist or not, by using the path of our choosing. We are able to use a parameter to filter out the object, or we can use the path of the object to determine it’s existance.

Get-Help Test-Path

    Test-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType {Any | Container | Leaf}] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>]  
    Test-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType {Any | Container | Leaf}] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>]  


Test-Path will return True if the object exist and False if the the object doesn’t exist. Here is a working script that uses the Test-Path
PowerShell Script: Copy File that Doesn’t Exist

Test-Path "F:\Test\FileExists.txt"

Test-Path "F:\Test\FiledoesntExists.txt"

We can use the wildcard function and the -include parameter to filter out objects in a directory. We need to provide the wildcard at the end of the path and also at the beginning of the file extensions.

Test-Path F:\Downloads\* -include *.ps1, *.exe[/code]

Make sure to follow the blog and come back every week for some more helpful hits and how to guides on using PowerShell.


One thought on “PowerShell: Test-Path cmdlet

  1. Pingback: PowerShell: Copy-Item Same Directory | ZOTech Consulting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s