EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Test-Path
 

PowerShell Test-Path

Priya Pedamkar
Article byPriya Pedamkar

Updated June 13, 2023

PowerShell Test-Path

 

 

Introduction to PowerShell Test-Path

Powershell test-path command checks for the path’s existence for all the elements. It always returns boolean values. It will return $True and $False. It will return $True as all the elements are there else; it will return $False. With the help of the Test-Path command, we can also identify the path types; that is, if the path is a container, leaf, or a terminal, it will always return $False if Path is whitespace, and it will return an error if the path is null. Real-Time uses, suppose you are checking any file in the directory, and you do not want to check, or you want to exclude one file from the directory because that particular file is in huge number, then you can use it.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax 1:

Test-Path
[-Path] <String of path to be tested>
[-Filter <String of Filter arguments >] [-Include <String of Path which need to include>] [-Exclude <String of path which need to exclude>] [-PathType <test path type(leaf, container or ny)>] [-IsValid(return boolean true or false)] [-Credential <PSCredential>] [-OlderThan <DateTime to check older than>] [-NewerThan <DateTime to check newer than>] [<CommonParameters>]

Syntax 2:

Test-Path
-LiteralPath <String of exact path>
[-Filter <String of path to be tested>] [-Include <String of Path which need to include>] [-Exclude <String of path which need to exclude>] [-PathType <test path type(leaf, container or ny>] [-IsValid(return boolean true or false)] [-Credential <PSCredential>] [-OlderThan <DateTime to check older than>] [-NewerThan <DateTime to check newer than>] [<CommonParameters>]

Parameters

1. -Exclude: This command disallows or omits a defined item. This parameter will be used for path parameters. We can define path element or any pattern like “*.txt,””*.pdf,” etc. And these defined patterns can be excluded from the path.

2. -Filter: Allow our command to define a filter for checking path elements. For example, if we have huge file systems and want to check whether the path exists, we can define some filter conditions. We can use Wildcard for it.

3. -Include: This command we use to check for specific path attributes. In general, it will include the attribute passed into this command. Wildcard string attributes are also allowed in this case. We can use path element or attribute like “*.txt”,”*.pdf” etc.

4. -IsValid: It checks for the path syntax; this command does not worry about whether the path exists. It will simply validate the path syntax, so if the path syntax is valid, it will return $True; if the path syntax is not valid, it will return $False.

5. -LiteralPath: This is also one kind of path checking, but here, in this case, we have to pass the exact match path; we can not use “*.txt”; we must pass a path like “ranjan.txt,” which is an exact name not like matching. The good thing is that we can also use escape characters in these cases. In the case of escape characters, we should use single quotation marks. As a single quotation, inform PowerShell to treat characters as escape characters.

6. -NewerThan: Defines any time as DateTime; simply, it will check for the file creation dates, it checks if the date of the file creation date, and if the date of creation is newer than the argument date provided, then it will return true. For example, take an argument passed as “August 13, 2019,” and the date of file creation is “August 15, 2019” Then it will return true as file creation is newer than the argument pass date.

7. -OlderThan: Defines any time as DateTime; simply, it will check for the file creation dates; it checks if the date of file creation date, and if the date of creation is older than the argument date provided, it will return true. For example, take an argument passed as “August 13, 2019,” and the date of file creation is “August 15, 2019,” then it will return false as an argument passed as newer than the creation date.

8. -Path: Defines any path that will be tested. We can also use a wildcard in this case. Also, if the path has spaces between them, we can use a single quotation to inform PowerShell.

9. -PathType: It defines the exact types of the given element in the path. In simple it will check for paths elements types. It will return a boolean value. If the path of a given element is of the same type we defined in the command, then it will return $True, and if the type of path is not the same as what we defined in the command, then it will return $False. This command will take the parameters below, like the PathType command value.

  • Any container: It contains elements like registry and directories.
  • Lead Item: This element will not contain attributes like any file.
  • Combination: It can be both also, which is any container or any leaf.

Examples of PowerShell Test-Path

Below are the examples of PowerShell Test-Path:

Example #1

The below command is one example where we check for any files inside “ranjan1” directories other than “*.txt.”

Test-Path -Path "./ranjan1/" -Exclude *.txt

Output:

powershell test path 1

Test-Path -Path "./ranjan1/" -Exclude *.pdf

Output:

powershell test path 2

Test-Path -Path "./ranjan1/" -Include *.pdf

Output:

powershell test path 3

cd ./ranjan1/
ls

Output:

checking the file

Example #2

Below is an example for checking PathType. We contain PathType for $PROFILE by passing arguments like Any, Leaf, and Container.

Test-Path -Path $PROFILE -PathType Any

Output:

checking path type

Test-Path -Path $PROFILE -PathType Container

Output:

path type container

Test-Path -Path $PROFILE -PathType leaf

Output:

powershell test path 7JPG

Example #3

If the path is there, then it will return True; if the path does not exist, it will return False.

Test-Path -Path "./ranjan1/"

Output:

powershell 8

Test-Path -Path "./ranjan2/"

Output:

powershell 9

Test-Path -Path "./ranjan3/"

Output:

powershell 10

ls

Output:

powershell 11

Example #4

Here we are checking the file’s date of creation. It could be older or newer. I have created the file test.txt, it was created in 2019 before August month, and we are checking it with various dates bypassing them.

Test-Path ./test2.txt -NewerThan "August 13, 2019"

Output:

file date of creation

Test-Path ./test2.txt -OlderThan "August 13, 2019"

Output:

file date of creation 2

Test-Path ./test2.txt -NewerThan "August 13, 2020"

Output:

powershell op4.3

Test-Path ./test2.txt -NewerThan "Jan 13, 2020"

Output:

powershell op4.4

Test-Path ./test2.txt -NewerThan "July 13, 2020"

Output:

powershell op4.5JPG

ls

Output:

powershell op4.6JPG

Conclusion – PowerShell Test-Path

From above all, we learned that the Test-Path command can be used to either check path type or to check the path syntax. We can identify if the path is a container, leaf, or mixed(Any).

Recommended Articles

This has been a guide to PowerShell Test-Path. Here we discuss the introduction, parameters, and examples of the Powershell test path. You may also have a look at the following articles to learn more –

  1. Try-catch in PowerShell
  2. cmdlets in PowerShell
  3. PowerShell Sort-Object
  4. Variable in PowerShell
  5. Examples to Implement PowerShell New-Item
  6. Learn the PowerShell Rename-Item
  7. PowerShell Break | Examples
  8. Parameters of PowerShell Convert to String

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW