Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Created October 23, 2017 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/dfe687a06c4e815afb9841be707c837c to your computer and use it in GitHub Desktop.
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
$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