Recurse with Grep (SLS in PS 5.1)

The diffculty at finding this was unnecessary Linux guys… sheesh!

grep ssh *\*.ps1
(grep ssh *\*.ps1).path|select -Unique

The first line shows how to recurse a sls/grep search in Powershell for specific content of PS scripts, in subdirectories, not the .\, while the second line gives an example on how to get the list you can then call a program against, like ISE or mv, with the full path and name of the file, to find it correctly in the call. This gets REALLY handy when you have hundreds of scripts and haven’t really organized them into folders yet. like this:

(grep ssh *\*.ps1).path|select -Unique|foreach{mv $_ .\ssh}

So this example above shows how to use the recursive GREP search to make sure all related scripts are moved to the SSH folder you created.

Nice, huh?

BTW, both these work:

D:\PowerShell>(grep ssh .*\Power*.ps1).path|select -Unique
D:\PowerShell\Gold_Scripts_DO_NOT_BREAK\Powershell-Helps.ps1
D:\PowerShell>(grep ssh *\Power*.ps1).path|select -Unique
D:\PowerShell\Gold_Scripts_DO_NOT_BREAK\Powershell-Helps.ps1

Now, if you need to see the lines, do the search with all the properties and narrow it afterwards for what you want:

D:\PowerShell>grep ssh .*\Power*.ps1|select -Unique

Here is how to do the same search with the root included and with an exclude to ignore a specific fully qualified folder path:

Get-ChildItem -Path 'D:\PowerShell' -Recurse -Filter '*.ps1' | 
    Where-Object { $_.FullName -notlike 'D:\PowerShell\FolderToNotSearch\*' } | 
    Select-String -Pattern 'ssh' | 
    Select-Object -ExpandProperty Path -Unique

Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.