Tuesday, 11 July 2017

Using PowerShell to grep

PowerShell is not Unix of course, but nevertheless, it does have a useful cmdlet to emulate the very useful Unix grep command. This is achieved by the Select-String (finds text in strings and files) cmdlet.

In order to learn more about PowerShell and grep, have a read of Grep, the PowerShell way. It's an excellent article on a comparison between the two. When you read the article, you'll see reference to sls. This is just an alias for Select-String which saves you having to type out the cmdlet name in full.



PS> Get-Alias sls

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           sls -> Select-String
 
I use Select-String myself from time to time but never seem to remember the best way of using it. This article serves as an aide-memoire.


See also


Grep, the PowerShell way
https://communary.net/2014/11/10/grep-the-powershell-way/

Select-String (finds text in strings and files)
https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.utility/select-string



Keywords: unix grep sls select-string search text file