$response = Read-Host -Prompt "Set the archive bit on this file?" $files = Select-String -Path "d:\script\*.txt" -Pattern $pattern Here is an example that greps for a string and uses the results in a loop to determine if some action should be taken: $pattern = "tachytelic" Then you can simply pipe to Select-String like this:Ĭat "D:\Script\Lorem-Ipsum.txt" |grep "tachytelic" Loop through results from Select-String Set-Alias -Name grep -Value Select-String ![]() If you want to make it more like Unix/Linux, add an alias to the Select-String cmdlet: Just like grep, you can pipe to Select-String like this: Get-ChildItem -Path "D:\Script\*.txt" -Recurse | Select-String -Pattern 'tachytelic' Piping to Select-String Unlike grep, Select-String does not have the ability to search recursively, but you can pipe output to it from Get-ChildItem, like this: Select-String -Path "D:\script\*.txt" -Pattern 'Tachytelic' Grep Recursively with Get-Childitem Select-String -Path "D:\script\Lorem-Ipsum.txt" -Pattern 'Tachytelic' ![]() ![]() To grep a simple text file is as easy as: PowerShell brings the functionality of grep with the Select-String cmdlet. Unix and Linux have had the incredibly powerful grep tool for decades but windows has always been lacking.
0 Comments
Leave a Reply. |