Every once in a while, we've got to search our scripts, often to make a particular update.
Sadly, we're often falling back on Select-String to do this.
This isn't ideal, because this means we lose the context around our scripts.
So why not make a quick tool to search PowerShell ScriptBlocks using the Abstract Syntax Tree?
Let's start simple. As a general rule, we don't want to use Invoke-Expression in our scripts.
We can find any matching part of the syntax tree:
{
iex "'I could do anything'"
}, {
Invoke-Expression "IsBad, ok"
}, {
"this is fine"
} |
Search-Script -For "^(iex|Invoke-Expression)"Suppose we want to update any scripts that use Invoke-WebRequest, in order to address CVE-2025-54100.
We can use a little bit of Regex to identify them, but it gets a lot trickier to write a pattern that will find if it's already fixed or not.
If we could just see that they use -UseBasicParsing, then it's already been fixed.
This little script helps us tell the difference:
{
Invoke-WebRequest -Url $NotOK # not yet fixed
},{
Invoke-WebRequest -Url $OK -UseBasicParsing # already fixed
} |
Search-Script -For {
param($ast)
if (-not $ast.CommandElements -or (
$ast.CommandElements[0] -notmatch 'Invoke-WebRequest|curl|iwr'
)) {
return $false
}
if (-not ($ast.CommandElements -match '-UseBasicParsing')) { return $true }
return $false
}You can install SearchScript from the PowerShell gallery
Install-Module SearchScript -Scope CurrentUser -ForceOnce installed, you can import the module with:
Import-Module SearchScript -PassThruYou can also clone the repo and import the module locally:
git clone https://github.com/StartAutomating/SearchScript/
cd ./SearchScript
Import-Module ./ -PassThruSearchScript has 1 function
Searches PowerShell scripts, using the Abstract Syntax Tree.
This can quickly and easily find any part of any script in any file.
| Name | Type | Description |
|---|---|---|
| Script | ScriptBlock | The script to search. |
| For | PSObject | What we're searching for. Can be a ScriptBlock, string, regex, type, or ast function. If a string is provided, it will be treated as a literal, unless it starts and ends with /. |
| Shallow | SwitchParameter | If set, will perform a shallow search. By default will search a scriptblock and nested blocks. |
Get every part of every script in this module.
Get-Command -Module SearchScript | SearchScriptGet every part of every script in the current directory
dir *.ps1 | Get-Command { $_ } | SearchScriptGet every type reference in the current directory
dir *.ps1 |
Get-Command { $_ } |
SearchScript -For { param($ast) $ast.TypeName }Get every type reference in the current directory then get their reflected type
dir *.ps1 |
Get-Command { $_ } |
SearchScript -For {
param($ast) $ast.TypeName.GetReflectionType
} |
Foreach-Object {
$_.TypeName.GetReflectionType()
}Get every command reference in the module search script
Get-Command Search-Script |
Search-Script -For {
param($ast) $ast -is [Management.Automation.Language.CommandAst]
}Get every [Management.Automation.Language.VariableAst] in Search-Script
Get-Command Search-Script |
Search-Script -For ([Management.Automation.Language.VariableAst])Search Script for every [type]
Get-Command Search-Script |
Search-Script -For ([type])Search-Script for every [ScriptBlock]
Get-Command Search-Script |
Search-Script -For ([ScriptBlock])Get every [ScriptBlock] and [string] reference in Search-Script
Get-Command Search-Script |
Search-Script -For ([ScriptBlock], [string])Get every [IComparable] reference in Search-Script
Get-Command Search-Script |
Search-Script -For ([IComparable])Search for scripts that may be impacted by CVE-2025-54100
Search-Script { Invoke-WebRequest } {
param($ast)
if (-not $ast.CommandElements -or (
$ast.CommandElements[0] -notmatch 'Invoke-WebRequest|curl|iwr'
)) {
return $false
}
if (-not ($ast.CommandElements -match '-UseBasicParsing')) { return $true }
return $false
}