Ever wanted to get a sorted list of all variables used inside a script? Use this function: simply call Get-ScriptVariables and supply a path to your PowerShell script. You will then get back a sorted list of all variable names found in that script:
function Get-ScriptVariables($path) {
$result = Get-Content $path |
ForEach-Object { if ( $_ -match '(\$.*?)\s*=') {
$matches[1] | Where-Object { $_ -notlike '*.*' }
}
}
$result | Sort-Object | Get-Unique
}
Get-ScriptVariables "$home\myscript.ps1"
Posted
Apr 10 2009, 08:00 AM
by
ps1