Question

I've found quite a few questions/answers and other types of posts that talk about using alias to substitute, or alias, grep for findstr. While this is useful, one of things I like about grep in many terminal environments is that the found text is highlighted; often in red.

findstr /A:color seems to imply that this should be possible and even easy...but I've yet to make it work in a simple statement like:

set-alias grep "findstr /A:color F0"

Is there way to do this?

Thanks

Was it helpful?

Solution

I seldomely create new aliases, so there might very well be other (better) ways than the one I write above below, but this way seems to work.

The help section on Set-Alias, for the parameter Value states:

-Value

   Specifies the name of the cmdlet or command element that is being aliased.

So it expects the aliased command to either be a cmdlet or a command element.

If you do Get-Help Set-Alias -Examples you'll see the last example which does something similar to what you are hoping for, to call a command and specify some hard coded parameters. It does this with the following code:

PS C:\>function CD32 {set-location c:\windows\system32}
PS C:\>set-alias go cd32

So what you need to do is to create a function which does what you want and then alias that command. An example of this would be:

function Search-Text
{
    & c:\Windows\system32\findstr.exe /A:F0 $Args
}

Then you could alias that new function to grep like:

Set-Alias grep Search-Text

And finally just call it like:

grep mysearchword file1.txt file2.txt

Note: The above code works fine for me when I run it in the ordinary PowerShell shell (probably since this is actually the ordinary command shell with some extra stuff), but does not get the expected colored printing in the ISE (obviously the ISE handles the host printing differently).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top