Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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, 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading