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

Comments

Silence wrote re: Auto-Documenting Script Variables
on 08-22-2014 3:40 AM

   if($tabID -eq 1)     #$tabID = 1: "xxxxxxxxxxxxxxxxxxx"

   {

       $vsSKU = "VS2013"

   }

   elseif($tabID -eq 2) #$tabID =2: ""xxxxxxxxxxxxxxxxxxx""

   {

       $vsSKU = "VS2012"

   }

my script include above code and it returns like below:

$tabID -eq 1)     #$tabID

$tabID -eq 2) #$tabID

looks the regex should adjust in some special place

Copyright 2012 PowerShell.com. All rights reserved.