#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-MyVariable.ps1 # Version: 2.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 05/29/2012 # Keywords: # Comments: # # # "Those who neglect to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-MyVariable { <# .Synopsis Get all user defined variables .Description This function will return all variables not defined by PowerShell or by this function itself. The default is to return all user-created variables from the global scope but you can also specify a scope such as script, local or a number 0 through 5. The command will also display the value type for each variable. If you want to supress this output use the -NoTypeInformation switch. This function is designed to work with the PowerShell console, NOT the ISE. .Parameter Scope The scope to query. The default is the Global scope but you can also specify Local, Script, Private or a number between 0 and 3 where 0 is the current scope, 1 is the parent scope, 2 is the grandparent scope and so on. .Parameter NoTypeInformation If specified, suppress the type information for each variable value. .Example PS C:\> Get-MyVariable Name Value Type ---- ----- ---- a 5/28/2012 8:45:25 AM DateTime bigp ps | where {$_.ws -gt 1... ScriptBlock cert [Subject]... X509Certificate2 dirt Param([string]$Path=$en... ScriptBlock disk Param ([string]$compute... ScriptBlock jdh Jeffery D. Hicks String godot7 System.Management.Autom... PSCredential ... .Example PS C:\> Get-MyVariable | Export-Clixml myvar.xml You can then import this xml file in another session to restore these variables. PS C:\> import-clixml .\myvar.xml | >> foreach {set-variable -Name $_.name -Value $_.value} >> .Example PS C:\> function foo { . c:\scripts\get-myvariable2.ps1;$a=4;$b=2;$c=$a*$b;get-MyVariable -notypeinformation -scope 1 -verbose;$c} This sample function dot sources the script with this function. Within the function, Get-MyVariable is called specifying scope 1, or the parent scope. Scope 0 would be the scope of the Get-MyVariable function. Here's the result. PS C:\> foo VERBOSE: Getting system defined variables VERBOSE: Found 49 VERBOSE: Getting current variables in 1 scope VERBOSE: Found 27 VERBOSE: Filtering variables Name Value ---- ----- a 4 b 2 c 8 VERBOSE: Finished getting my variables 8 .Example PS C:\> get-myvariable | where {$_.type -eq "Scriptblock"} | Select name,value Name Value ---- ----- bigp ps | where {$_.ws -gt 100mb} dirt Param([string]$Path=$env:temp) Get-C... disk Param ([string]$computername=$env:co... run gsv | where {$_.status -eq "running"} up Param([string]$computername=$env:com... Get all my variables that are scriptblocks. .Notes NAME: Get-MyVariable AUTHOR: Jeffery Hicks VERSION: 2.0 LASTEDIT: 05/29/2012 Learn more with a copy of Windows PowerShell 2.0: TFM or get started on PowerShell v3 with PowerShell in Depth. .Link http://jdhitsolutions.com/blog/2012/05/get-my-variable-revisited .Link Get-Variable About_Variables About_Scope .Inputs None .Outputs Variable or custom object #> [cmdletbinding()] Param( [Parameter(Position=0)] [ValidateSet("Global","Local","Script","Private",0,1,2,3)] [ValidateNotNullOrEmpty()] [string]$Scope="Global", [switch]$NoTypeInformation ) if ($psise) { Write-Warning "This function is not designed for the PowerShell ISE." } else { Write-Verbose "Getting system defined variables" #get all global variables from PowerShell with no profiles $psvariables=powershell -noprofile -COMMAND "& {GET-VARIABLE | select -expandproperty name}" Write-Verbose "Found $($psvariables.count)" <# find all the variables where the name isn't in the variable we just created and also isn't a system variable generated after the shell has been running and also any from this function #> Write-Verbose "Getting current variables in $Scope scope" $variables=Get-Variable -Scope $Scope Write-Verbose "Found $($variables.count)" Write-Verbose "Filtering variables" #define variables to also exclude $skip="LastExitCode|_|PSScriptRoot|skip|PSCmdlet|psvariables|variables|Scope|this" #filter out some automatic variables $filtered=$variables | Where {$psvariables -notcontains $_.name -AND $_.name -notmatch $skip} if ($NoTypeInformation) { #write results with not object types $filtered } else { #add type information for each variable Write-Verbose "Adding value type" $filtered | Select-Object Name,Value,@{Name="Type";Expression={$_.Value.GetType().Name}} } Write-Verbose "Finished getting my variables" } } #end function Set-Alias -Name gmv -Value Get-MyVariable