简体   繁体   中英

Select-String -Pattern Wildcard Match

I have a number of .ps1 scripts in a folder, example file content shown below:

file1.ps1
# Do stuff
Get-Acl -Path HKLM:\

file2.ps1
# Do stuff
Get-ADUser -Identity TestUser

Using the command Select-String I want to find.ps1 scripts with AD cmdlets only, eg Get-ADUser , Get-ADGroup etc.. Example:

Get-ChildItem -Path C:\MyScripts | Select-String -Pattern "Get-AD*"

This returns file1.ps1 and file2.ps1, expected output is file2.ps1 only

What am I doing wrong?

Select-String uses regular expressions, not wildcard patterns, and in that context * means "0 or more of the previous element" - so it's looking for the literal string Get-A followed by 0 or more D 's - and Get-Acl does indeed satisfy this constraint.

To describe a substring starting with Get-AD followed by some more letters, you can do:

Get-ChildItem -Path C:\MyScripts | Select-String -Pattern "Get-AD\p{L}+"

\p{L} descibes any letter , + means "1 or more of the previous element", so Get-AD followed by 1 or more letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM