Auto-Documenting Script Variables

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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
Copyright 2012 PowerShell.com. All rights reserved.