PowerShell Use the Test-path command to check for an example of whether a file or folder path exists _powershell

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.