Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all previously-loaded PowerShell variables

Is there a PowerShell command to list all previously loaded variables?

I am running some PowerShell scripts in Visual Studio and would like to list all variables that are available in the current PowerShell session.

I have tried the command:

ls variable:*; 

But it returns the following:

System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable System.Management.Automation.PSVariable 
like image 450
Zooking Avatar asked Sep 17 '12 19:09

Zooking


People also ask

Is there a way to see PowerShell history?

The Get-History cmdlet gets the session history, that is, the list of commands entered during the current session. PowerShell automatically maintains a history of each session. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable.

How do I list all variables in PowerShell?

To view all environment variables in the current PowerShell session, you can run the command: Get-ChildItem Env: This is equivalent to running the Set command in Cmd.exe. returns The ALLUSERSPROFILE variable is C:\ProgramData.

What does $_ do in PowerShell?

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.

How do I get a list of environment variables in PowerShell?

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.


1 Answers

ls variable:* should work, or Get-Variable. If these are resulting in bad output, it's due to a poorly-implemented host, not with powershell itself. If you open the standard console host (run powershell.exe), you will see that these work fine.

If you need to work around a bad host, you might have better luck dumping everything to explicit strings:

Get-Variable | Out-String 

or

Get-Variable |%{ "Name : {0}`r`nValue: {1}`r`n" -f $_.Name,$_.Value } 
like image 151
latkin Avatar answered Oct 06 '22 11:10

latkin