• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Short and Concise Guides for IT Professionals

MENUMENU
  • HOME
  • ALL TOPICS
    • Active Directory
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Virtualization
    • Web
    • Tools
  • ABOUT
  • SUBSCRIBE
Home » Grep command in PowerShell (recursive)

Grep command in PowerShell (recursive)

May 10, 2021 - by Zsolt Agoston - last edited on May 10, 2021

Description

Simulating the Unix grep command to browse through all the files in the current location and all the subfolders for a specific string.

Script

Function grep {
	# Get pattern to search in files
	$patt = $args[0]
	
	# Run recursive search on all files in the current location and in subfolders
	Get-ChildItem -File -Recurse | %{
		If (Select-String -Path $_.FullName -Pattern $patt -SimpleMatch -Quiet) {
			Write-Host -NoNewline "$(Split-Path $_.FullName -Parent)\"
			Write-Host -ForegroundColor green "$(Split-Path $_.FullName -Leaf)"
		}
	}
}

Example

Searching for all the files that contain the string "created"

# Unix version:
grep -r "created"

# PowerShell version:
grep "created"
Grep command in PowerShell (recursive)

Reader Interactions

Community Questions Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Description
  • Script
  • Example

  • Terms of Use
  • Disclaimer
  • Privacy Policy