Powershell-use regular expressions to find files

Source: Internet
Author: User
This article describes how to use regular expressions to search for files in PowerShell. the regular expressions in PowerShell are the same as those in other languages of Microsoft, which is very convenient to use. All PS versions supported

Get-ChildItem does not support advanced file filtering. It can only use simple wildcards, but cannot use regular expressions.

To solve this problem, we can use the-match command to filter.

In the following example, all files in the windows directory that contain at least two consecutive numbers are obtained, and the file name length cannot exceed 8 characters:

Get-ChildItem-Path $ env: windir-Recurse-ErrorAction SilentlyContinue | Where-Object {$ _. baseName-match '\ d {2}'-and $ _. name. length-le 8}

Note that the "BaseName" attribute of the file does not include the extension, so that the number appears in the extended name and will not be counted.

More Powershell-use regular expressions to search for files!

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.