August 2nd, 2013

PowerTip: Find the Number of Variables In-Scope

Doctor Scripto
Scripter

Summary: Find the number of variables in-scope in your function.

Hey, Scripting Guy! Question How can I see how many variables are available in-scope and out-of-scope in my Windows PowerShell function?

Hey, Scripting Guy! Answer Use the Get-Variable cmdlet, the Count property, and the –Scope parameter:

All variables

PS C:\> function myvar {(Get-Variable).count}

PS C:\> myvar

49

Variables in-scope

PS C:\> function myvar {(Get-Variable -Scope 0).count}

PS C:\> myvar

25 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.