Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell - Remove all variables

I want to remove all user-created variables at the start of a script.

Currently I am doing Remove-Variable -Name * but it tries to delete all system variables as well, resulting in lot of error messages.

Any other way?

like image 977
Ammar Avatar asked Jul 16 '13 13:07

Ammar


People also ask

How do I clear PowerShell?

How to clear the PowerShell screen. As discussed earlier, the PowerShell clear screen action is supported by a function Clear-Host and its two aliases cls and clear.

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.


1 Answers

Since all the system variables are read-only or constant anyway you can just silence the errors:

Remove-Variable * -ErrorAction SilentlyContinue

But admittedly, you should probably exclude a few anyway:

Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
like image 188
Joey Avatar answered Oct 18 '22 06:10

Joey