Home » PowerShell » PowerShell Get Variables

PowerShell Get Variables

The Get-Variable cmdlet is used to get PowerShell variables in the current scope. You can get PowerShell variable names or values only.

PowerShell variables is a unit of memory in which it stores the value. PowerShell variables are text string that starts with a dollar sign ($) like $test,$my_func etc.

In this article, we will discuss how to use the Get-Variable cmdlet to get variables in PowerShell and list all variables created in the current scope.

PowerShell Get-Variable

Get-Variable cmdlet in PowerShell list variables.

Syntax

Get-Variable
   [[-Name] <String[]>]
   [-ValueOnly]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Scope <String>]
   [<CommonParameters>]

Parameters

-Name: Specify the name of the variable. You can use wildcards to get variables.

-ValueOnly: Gets the value of the variable.

-Scope: Specify the variables in the scope. Acceptable values for parameters are Global, Local, and Script or numeric.

Get Variables in PowerShell

You can get the variables in PowerShell that are created in the current scope using the Get-Variable cmdlet.

Get-Variable proc*

In the above PowerShell script, the Get-Variable gets the variables with names starting with the proc*. It also gets the values of the variables.

The output of the above command show variables with their values. In the below output, it displays PowerShell $proces variable with its value.

PowerShell Get Variable
PowerShell Get Variable

Get PowerShell Variable Values by Letter

Using the Get-Variable cmdlet ValueOnly parameter, you can get variable values in PowerShell by letter.

Get-Variable te* -ValueOnly

In the above PowerShell script, Get-Variable uses –ValueOnly parameter to show variable values that have names starting with te.

The output of the above script to get variable values is:

Welcome to
ShellGeek Website

Get Variables in PowerShell by Multiple Letters

You can get variables in PowerShell by multiple letters using the Get-Variable cmdlet.

Get-Variable t*, m*

In the above PowerShell script, the Get-Variable cmdlet gets the variables that have names starting with either t or m.

The output of the above script show variable with their values.

PS C:\> Get-Variable t*, m*                                                                                             
Name                                    Value
----                                           ----
test                                         Shell
teststring                               Welcome to ...
true                                         True
MaximumAliasCount              4096
MaximumDriveCount              4096
MaximumErrorCount              256
MaximumFunctionCount           4096
MaximumHistoryCount            4096
MaximumVariableCount           4096
my_profile                     ShellGeek
MyInvocation                   System.Management.Automation.InvocationInfo

Get Variables in PowerShell by Scope

To get variables in PowerShell by scope, use the Get-Variable -Scope parameter.

Get-Variable -Scope Local 

In the above PowerShell script, it gets variables that are defined in the local scope.

Conclusion

I hope the above article on how to get variables in PowerShell using the Get-Variable is helpful to you.

You can find the variables in the current or global, script scope using the Scope parameter.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment