Home > OS >  Select-String -Pattern Wildcard Match
Select-String -Pattern Wildcard Match

Time:01-28

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, e.g. 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?

CodePudding user response:

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.

  •  Tags:  
  • Related