Auto-Documenting Script Variables

by Apr 10, 2009

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 “$homemyscript.ps1”