-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrepWindows.ps1
More file actions
90 lines (73 loc) · 2.93 KB
/
GrepWindows.ps1
File metadata and controls
90 lines (73 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<#
.SYNOPSIS
Recursively searches all files on all drives for a specified search term (like "John").
Results are written to a text file in C:\Temp.
.DESCRIPTION
This script is similar to a Linux grep -r, but for Windows using PowerShell’s Select-String.
It:
1) Gathers all FileSystem drives.
2) Recursively enumerates all files on those drives.
3) Uses Select-String to search for the pattern in each file.
4) Logs all matches (file path + matched line) to an output file.
.PARAMETER SearchTerm
The text pattern you want to search for (e.g., "John").
.PARAMETER OutputFile
The path where search results will be saved. Defaults to "C:\Temp\<SearchTerm>_SearchResults.txt".
.EXAMPLE
.\Search-AllFiles.ps1 -SearchTerm "John"
Searches for “John” in all files across all drives, logging matches to C:\Temp\John_SearchResults.txt.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SearchTerm,
[Parameter(Mandatory=$false)]
[string]$OutputFile = "C:\Temp\$($SearchTerm)_SearchResults.txt"
)
# Create output directory if it doesn’t exist
$OutDir = Split-Path $OutputFile -Parent
if (!(Test-Path $OutDir)) {
New-Item -ItemType Directory -Path $OutDir -Force | Out-Null
}
# Clear or create the output file
New-Item -Path $OutputFile -ItemType File -Force | Out-Null
Write-Host "Searching all files on all drives for pattern: '$SearchTerm'"
Write-Host "Results will be saved in $OutputFile"
Write-Host "This may take a long time, depending on system size..."
# Get all FileSystem drives
$Drives = (Get-PSDrive -PSProvider FileSystem).Name
# We’ll store all matches in $allMatches
$allMatches = @()
foreach ($drive in $Drives) {
$rootPath = "$drive`:\" # e.g., C:\, D:\, etc.
Write-Host "Scanning drive: $rootPath ..."
try {
# Recursively get all files
$files = Get-ChildItem -Path $rootPath -Recurse -Force -ErrorAction SilentlyContinue `
| Where-Object { -not $_.PSIsContainer } # keep only files
if ($files) {
# Search the files for the pattern
$matches = $files | Select-String -Pattern $SearchTerm -CaseSensitive:$false -SimpleMatch -ErrorAction SilentlyContinue
if ($matches) {
$allMatches += $matches
}
}
}
catch {
# If something catastrophic happens (like drive read error), we handle it.
Write-Host "Error scanning drive $rootPath : $_"
}
}
if ($allMatches) {
Write-Host "`nWriting matches to $OutputFile ..."
# Format: path:lineNumber: matchedLine
# If you want more detailed info, you can tweak how you output.
foreach ($match in $allMatches) {
$lineOutput = "{0}:{1}:{2}" -f $match.Path, $match.LineNumber, $match.Line
$lineOutput | Out-File -Append $OutputFile
}
Write-Host "`nSearch complete! Found $($allMatches.Count) matches. See $OutputFile."
}
else {
Write-Host "`nNo matches found for '$SearchTerm'."
}