Created
October 23, 2017 18:45
-
-
Save joerodgers/dfe687a06c4e815afb9841be707c837c to your computer and use it in GitHub Desktop.
Search all PowerShell Scripts in a Directory (and sub-directories) for a Specific String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$pattern = "Get-SPSite" | |
$path = "C:\_scripts\SharePoint\OnPrem" | |
$files = Get-ChildItem -Path $path -Recurse -Include "*.ps1", "*.psm1" | |
$scanned = @() | |
foreach( $file in $files ) | |
{ | |
if( $scanned -notcontains $file.Directory.FullName -and $file.Directory.Parent.FullName -eq $path ) | |
{ | |
$scanned += $file.Directory.FullName | |
Write-Host "Scanning: $($file.DirectoryName)" -ForegroundColor Cyan | |
} | |
else | |
{ | |
Write-Verbose "Scanning: $($file.DirectoryName)" | |
} | |
$patternMatches = Get-Content -Path $file.FullName | Select-String -Pattern $pattern | |
if( $patternMatches ) | |
{ | |
Write-Host "`t$($patternMatches.Matches.Count) match(es): $($file.FullName)" -ForegroundColor Green | |
} | |
else | |
{ | |
Write-Verbose "No Match: $($file.FullName)" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment