Powershell

How to Use the Get-Variable (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

A variable is a memory unit that is used to store values in it. A variable is a text string that starts with the dollar sign “$” in PowerShell. The “Get-Variable” cmdlet is used to get the variables in the current PowerShell console. Moreover, the values of a variable can also be retrieved by specifying the “-ValueOnly” parameter along with the “Get-Variable” cmdlet.

This guide will explain the “Get-Variable” cmdlet with the help of various examples.

How to Use the Get-Variable (Microsoft.PowerShell.Utility) Cmdlet in PowerShell?

The “Get-Variable” cmdlet can be used to retrieve the variables in PowerShell. First, use the “Get-Variable” cmdlet and assign it a variable name to get a variable.

To learn further usage of the “Get-Variable” cmdlet, overview the provided examples.

Example 1: Use the “Get-Variable” Cmdlet to Get a Variable by its Name
First, use the “Get-Variable” to retrieve a variable by name. Then, type the parameter “-Name” and specify a variable name:

Get-Variable -Name PSHOME

Example 2: Use the “Get-Variable” Cmdlet to Get the Variable by the Specified Letter
To retrieve a variable by a particular letter, first, use the “Get-Variable” cmdlet. Then, specify the letter along with the “*” wildcard character:

Get-Variable ps*

Example 3: Use the “Get-Variable” Cmdlet to Get the Multiple Variables by Specified Letter
Two or more than two variables can be retrieved by specifying variables to the “Get-Variable” cmdlet separated by a comma:

Get-Variable ps*, e*

Example 4: Use the “Get-Variable” Cmdlet to Get Only the Values of Variables
Users can get the value of a variable by using the “-ValueOnly” parameter in PowerShell:

Get-Variable max* -ValueOnly

Example 5: Use the “Get-Variable” Cmdlet to Get Variables by Scope
Initially, use the “Get-Variable” cmdlet. Then, add the “-Scope” parameter and specify the “Local” value to get the variables by scope:

Get-Variable -Scope Local

We have provided detailed information about the “Get-Variable” cmdlet in PowerShell.

Conclusion

PowerShell’s “Get-Variable” cmdlet is used to retrieve the variables in the current session. It can also retrieve the values of a variable. This guide elaborated on the “Get-Variable” cmdlet with the help of various examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.