PowerShell FINDSTR eqivalent?

Question!

What's the DOS FINDSTR equivalent for PowerShell? I need to search a bunch of log files for "ERROR".



Answers

PowerShell has basically precluded the need for findstr.exe as the previous answers demonstrate. Any of these answers should work fine.

However, if you actually need to use findstr.exe (as was my case) here is a PowerShell wrapper for it:

Use the -Verbose option to output the findstr command line.


function Find-String
{
    [CmdletBinding(DefaultParameterSetName='Path')]
    param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]
        $Pattern,

        [Parameter(ParameterSetName='Path', Mandatory=$false, Position=1, ValueFromPipeline=$true)]
        [string[]]
        $Path,

        [Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [Alias('PSPath')]
        [string[]]
        $LiteralPath,

        [Parameter(Mandatory=$false)]
        [switch]
        $IgnoreCase,

        [Parameter(Mandatory=$false)]
        [switch]
        $UseLiteral,

        [Parameter(Mandatory=$false)]
        [switch]
        $Recurse,

        [Parameter(Mandatory=$false)]
        [switch]
        $Force,

        [Parameter(Mandatory=$false)]
        [switch]
        $AsCustomObject
    )

    begin
    {
        $value = $Pattern.Replace('\', '\\\\').Replace('"', '\"')

        $findStrArgs = @(
            '/N'
            '/O'
            @('/R', '/L')[[bool]$UseLiteral]
            "/c:$value"
        )

        if ($IgnoreCase)
        {
            $findStrArgs += '/I'
        }

        function GetCmdLine([array]$argList)
        {
            ($argList | foreach { @($_, "`"$_`"")[($_.Trim() -match '\s')] }) -join ' '
        }
    }

    process
    {
        $PSBoundParameters[$PSCmdlet.ParameterSetName] | foreach {
            try
            {
                $_ | Get-ChildItem -Recurse:$Recurse -Force:$Force -ErrorAction Stop | foreach {
                    try
                    {
                        $file = $_
                        $argList = $findStrArgs + $file.FullName

                        Write-Verbose "findstr.exe $(GetCmdLine $argList)"

                        findstr.exe $argList | foreach {
                            if (-not $AsCustomObject)
                            {
                                return "${file}:$_"
                            }

                            $split = $_.Split(':', 3)

                            [pscustomobject] @{
                                File = $file
                                Line = $split[0]
                                Column = $split[1]
                                Value = $split[2]
                            }
                        }
                    }
                    catch
                    {
                        Write-Error -ErrorRecord $_
                    }
                }
            }
            catch
            {
                Write-Error -ErrorRecord $_
            }
        }
    }
}
By : skataben


This is not the best way to do this:

gci <the_directory_path> -filter *.csv | where { $_.OpenText().ReadToEnd().Contains("|") -eq $true }

This helped me find all csv files which had the | character in them.

By : deostroll


On a related note, here's a search that will list all the files containing a particular regex search or string. It could use some improvement so feel free to work on it. Also if someone wanted to encapsulate it in a function that would be welcome.

I'm new here so if this should go in it's own topic just let me know. I figured I'd put it her since this looks mostly related.

# Search in Files Script
# ---- Set these before you begin ---- 
$FolderToSearch="C:\" # UNC paths are ok, but remember you're mass reading file contents over the network
$Search="Looking For This" # accepts regex format
$IncludeSubfolders=$True #BUG: if this is set $False then $FileIncludeFilter must be "*" or you will always get 0 results
$AllMatches=$False
$FileIncludeFilter="*".split(",") # Restricting to specific file types is faster than excluding everything else
$FileExcludeFilter="*.exe,*.dll,*.wav,*.mp3,*.gif,*.jpg,*.png,*.ghs,*.rar,*.iso,*.zip,*.vmdk,*.dat,*.pst,*.gho".split(",")

# ---- Initialize ----
if ($AllMatches -eq $True) {[email protected]{AllMatches=$True}}
else {[email protected]{List=$True}}
if ($IncludeSubfolders -eq $True) {[email protected]{Recurse=$True}}
else {[email protected]{Recurse=$False}}

# ---- Build File List ---- 
#$Files=Get-Content -Path="$env:userprofile\Desktop\FileList.txt" # For searching a manual list of files
Write-Host "Building file list..." -NoNewline
$Files=Get-ChildItem -Include $FileIncludeFilter -Exclude $FileExcludeFilter -Path $FolderToSearch -ErrorAction silentlycontinue @RecurseParam|Where-Object{-not $_.psIsContainer} # @RecurseParam is basically -Recurse=[$True|$False]
#$Files=$Files|Out-GridView -PassThru -Title 'Select the Files to Search' # Manually choose files to search, requires powershell 3.0
Write-Host "Done"

# ---- Begin Search ---- 
Write-Host "Searching Files..."
$Files|
    Select-String $Search @SelectParam| #The @ instead of $ lets me pass the hastable as a list of parameters.  @SelectParam is either -List or -AllMatches
    Tee-Object -Variable Results|
    Select-Object Path
Write-Host "Search Complete"
#$Results|Group-Object path|ForEach-Object{$path=$_.name; $matches=$_.group|%{[string]::join("`t", $_.Matches)}; "$path`t$matches"} # Show results including the matches separated by tabs (useful if using regex search)

<# Other Stuff
    #-- Saving and restoring results
    $Results|Export-Csv "$env:appdata\SearchResults.txt" # $env:appdata can be replaced with any UNC path, this just seemed like a logical place to default to
    $Results=Import-Csv "$env:appdata\SearchResults.txt"

    #-- alternate search patterns
    $Search="(\d[-|]{0,}){15,19}" #Rough CC Match
#>


This video can help you solving your question :)
By: admin