EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Test-Path

PowerShell Test-Path

By Priya PedamkarPriya Pedamkar

PowerShell Test-Path

Introduction to PowerShell Test-Path

Powershell test-path command checks for the existence of the path for all the elements. It always returns boolean values. It will return $True and $False. Basically it will return $True as all the elements are there else it will return $False. With the help of Test-Path command, we can also identify the path types, that is if the path is a container or 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.

Syntax 1:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 we wanted to check if the path exists or not 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 if the path exists or not. It will simply validate the path syntax, so if the path syntax is valid it will return $True and 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 path like “ranjan.txt” which is an exact name not like matching. Good things here we can use escape characters also 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 than it will return true. Example take an argument passed as “August 13, 2019” and date of file creation is “August 15, 2019” than it will return true as file creation is newer than 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 argument date provided than it will return true. Example take an argument passed as “August 13, 2019” and date of file creation is “August 15,2019” than it will return false as an argument passed is newer than the creation date.

8. -Path: Defines any path which is going to be tested. We can also use a wildcard in this case. Also in case if the path has spaces in between them then 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 types which we defined in command than 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 below parameters like value for the PathType command.

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

Examples of PowerShell Test-Path

Below are the examples of PowerShell Test-Path:

Example #1

Below command is one example where we are checking if there are 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 are checking PathType for $PROFILE by passing arguments like Any, Leaf, 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 than it will return True and 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 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 Test-Path command can be used for either checking 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 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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW