-
-
Save mikedopp/d80632f1b05c77877ea57271c98b5baf to your computer and use it in GitHub Desktop.
A PowerShell function usedto search the text inside PowerShell scripts for a particular string
This file contains hidden or 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
Function Search-PSScripts{ | |
<# | |
.SYNOPSIS | |
Use to search the text inside PowerShell scripts for a particular string | |
.PARAMETER searchString | |
The string to search for inside the script file | |
.PARAMETER path | |
The folder path to search for PowerShell files in. Default to userprofile if not specified. | |
.PARAMETER recurse | |
Set to $false to exclude performing recursive search of folders | |
.EXAMPLE | |
Search-PSScripts -searchString "Get-Help" | |
Description | |
----------- | |
This command searches all of the script files in the user's profile path and its subdirectories. | |
.EXAMPLE | |
Search-PSScripts -searchString "Invoke-WebRequest" -path 'C:\Scripts' | |
Description | |
----------- | |
This command searches all of the script files in the current directory and its subdirectories. | |
.EXAMPLE | |
Search-PSScripts -searchString "Invoke-WebRequest" -path 'C:\Scripts' -recurse $false | |
Description | |
----------- | |
This command searches only the script files in the current directory. | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$searchString, | |
[Parameter(Mandatory=$false)] | |
[string]$path = $env:USERPROFILE, | |
[Parameter(Mandatory=$false)] | |
[boolean]$recurse = $true | |
) | |
$filter = "*.ps1","*.psm1" | |
# Confirm path is valid | |
if(!(Test-Path $path)){ | |
throw "'$path' is not a valid folder or is not accessible." | |
} | |
# Get the name of this script to exclude it | |
$Invocation = (Get-Variable MyInvocation -Scope 1).Value; | |
# Get all files in the path | |
Write-Progress -Activity "Search for PowerShell Script in $path" -Status "Depending on the number of scripts this may take some time" -PercentComplete 0 -id 1 | |
if($recurse){ | |
$files = Get-ChildItem $path -Recurse -include $filter -Exclude $Invocation.MyCommand -File -ErrorAction SilentlyContinue | |
} else { | |
$filter = $filter | %{$_.Replace('*','')} | |
$files = Get-ChildItem $path -filter '*.ps*1' -File -ErrorAction SilentlyContinue | ?{$filter -contains $_.Extension -and $_.Name -ne $Invocation.MyCommand} | |
} | |
$results=@();$i=1 | |
Foreach($file in $files){ | |
Write-Progress -Activity "Search for '$pattern' - $i of $($files.count)" -Status "Found: $($results.count)" -PercentComplete $(($i/$($files.count))*100) -id 1;$i++ | |
$found = Select-String -Path $file.fullname -pattern $searchString | |
if($found){ | |
Write-Verbose ($found | Out-String) | |
if($results.fullname -notcontains $file.fullname){ | |
$results += $file | |
} | |
} | |
} | |
Write-Progress -Activity "Done" -Id 1 -Completed | |
# Return found scripts sorted by last write time | |
$results | sort LastWriteTime | Ft LastWriteTime, FullName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment