#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-MyVariable.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 3/1/2011 # 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. 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. .Example PS C:\>Get-MyVariable Name Value ---- ----- f foo mail System.Management.Automation.PSCredential mycompany System.Management.Automation.PSCredential research System.Management.Automation.PSCredential s {System.ServiceProcess.ServiceController, Sy w 12762649 z 13 .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 { . s:\get-myvariable.ps1;$a=4;$b=2;$c=$a*$b;get-MyVariable -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 S:\> 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 .Notes NAME: Get-MyVariable AUTHOR: Jeffery Hicks VERSION: 1.0 LASTEDIT: 03/01/2011 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) .Link http://jdhitsolutions.com/blog/2011/03/get-my-variables/ .Link Get-Variable About_Variables About_Scope .Inputs None .Outputs Variable #> [cmdletbinding()] Param( [Parameter(Position=0)] [ValidateSet("Global","Local","Script","Private",0,1,2,3)] [ValidateNotNullOrEmpty()] [string]$Scope="Global") 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" $variables | Where {$psvariables -notcontains $_.name -AND $_.name -notmatch $skip} Write-Verbose "Finished getting my variables" } } #end function Set-Alias -Name gmv -Value Get-MyVariable