Checking whether a file or folder path exists is a feature that is often used in programming. PowerShell provides a very handy cmdlet for this requirement, which is test-path! Oh, look at a name, you know how professional it is. Take a look at the basic introduction to it first:
Test-path, check to see if the path exists.
Syntax: Test-path < path >
Description: The path here can be: file, folder, HKLM Path, environment variable env: path
Here are some examples to make it easier for us to understand:
Copy Code code as follows:
Test-path D:\q.txt
Test-path C:\Scripts\Archive-pathType Container
Test-path "Hkcu:\software\microsoft\driver Signing"
Test-path ALIAS:\GCI
Test-path Env:\username
Test-path C:\Scripts\Archive-pathType Leaf
Test-path C:\SCRIPTS\ARCHIVE\*.PS1
Test-path c:\scripts\archive\*-include *.ps1, *.vbs
Test-path c:\scripts\archive\*-include Test*.ps1, Test*.vbs
Test-path c:\scripts\archive\*-exclude *.ps1
Test-path c:\scripts\archive\*-exclude *.gif, *.jpg
Test-path d:\ Web Site backup \2013-04-*\cnsm* #检查2013-04 There are no CNSM items at the beginning of the directory
After reading, smart of you, do you think this thing is too TM powerful? Alas, that's why it's called PowerShell.
To say the return value, the return value of Test-path is true or false, so you can put it in the IF statement and the effect is very good.