Listing All Variables in All Scripts

by Jun 24, 2015

Ever wanted an inventory of variable names used in all open scripts in the PowerShell ISE?

Here is some code that you can run to create such a list:

$psise.CurrentPowerShellTab.Files |
  ForEach-Object {
        $errors = $null
        [System.Management.Automation.PSParser]::Tokenize($_.Editor.Text, [ref]$errors) |
        Where-Object { $_.Type -eq 'Variable'} |
        Select-Object -Property Content |
        Add-Member -MemberType NoteProperty -Name Script -Value $_.DisplayName -PassThru 
    } |
    Sort-Object -Property Content, Script -Unique

Twitter This Tip! ReTweet this Tip!