5

I want to find files containing the word "navbar" anywhere in files. I can do this using Mac's grep command like this:

grep -R "navbar" *

What's its equivalent in PowerShell 1.0?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
coure2011
  • 40,286
  • 83
  • 216
  • 349
  • 1
    Just an FYI, the path %windir%\system32\windowspowershell\v1.0 doesn't mean you have powershell 1.0 - this is an unfortunate side effect of an earlier botched versioning decision. This is the location for powershell 1, 2 and 3 (the latest as of right now.) - to see the powershell version, examine $psversiontable variable. If it does not exist, you have 1.0. If it does exist, look at the PSVersion property. – x0n Jun 20 '12 at 14:32

1 Answers1

7
findstr /s "navbar" *

It's a native command but should work well enough.

PowerShell 1.0 itself is a little tricky, as Select-String (the direct equivalent) only exists since 2.0, I think. So you'd have to make do with something like:

Get-ChildItem -Recurse |
  ForEach-Object {
    $file = $_
    ($_ | Get-Content) -cmatch 'navbar' |
      ForEach-Object { $file.Name + ':' + $_ }
  }

Short version:

ls -r|%{$f=$_;($_|gc)-cmatch'navbar'|%{$f.Name+":$_"}}

This is quite literally:

  1. Find all files recursively (the -R part).
  2. Read each file and print matching lines with their file name.
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 5
    Erm, poor users of outdated software. Install PowerShell v2 please. And `findstr` *works*. The rest was just to emulate its output. If you're only interested in the file where `navbar` appears (and not the line where it does) then `gci -r|?{(gc $_)-cmatch'navbar'}` does that. – Joey Jun 20 '12 at 06:16
  • 2
    Just an FYI, the path %windir%\system32\windowspowershell\v1.0 doesn't mean you have powershell 1.0 - this is an unfortunate side effect of an earlier botched versioning decision. This is the location for powershell 1, 2 and 3 (the latest as of right now.) - to see the powershell version, examine $psversiontable variable. If it does not exist, you have 1.0. If it does exist, look at the PSVersion property – x0n Jun 20 '12 at 14:32
  • 1
    Additionally, you could only have 1.0 if you're running server 2003 or windows xp (which I hope you're not!) – x0n Jun 20 '12 at 14:33
  • To determine the PowerShell version, see Stack Overflow question *[How to determine what version of PowerShell is installed?](http://stackoverflow.com/questions/1825585/how-to-determine-what-version-of-powershell-is-installed)*. – Peter Mortensen Sep 08 '13 at 20:06