Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear powershell session

Tags:

powershell

Is there a commandlet that clears the current powershell session variables that I have added?

I am using the Add-Type commandlet, and I am getting the error "Cannot add type. The type name already exists."

like image 818
BrokeMyLegBiking Avatar asked Dec 09 '10 22:12

BrokeMyLegBiking


People also ask

How do you refresh a session in PowerShell?

Use the Update-SessionEnvironment command to refresh the current PowerShell session with all environment settings possibly performed by Chocolatey package installs.


2 Answers

A possible "work around": Open a powershell window and then to run your script enter powershell .\yourScriptHere.ps1

This launches a new powershell instance which exits when your script exits. If you want to "play" in the new instance then change the invocation to powershell -NoExit .\yourScriptHere.ps1 and the new instance will not exit when the script completes. Enter exit when you need another restart and hit the "up arrow" key to get the previous command. All script output will appear in the same window. The overhead for starting a new powershell instance is low -- appears to be less than 1 second on my laptop.

like image 164
Bob Reynolds Avatar answered Sep 23 '22 07:09

Bob Reynolds


Unfortunately you can't unload .NET assemblies that have been loaded into the default AppDomain which is what Add-Type does. You can rename types or namespaces to limp along but at some point you just have to exit and restart PowerShell.

This is not a PowerShell limitation so much as it is a .NET/CLR limitation. You can load .NET assemblies into separate AppDomains which can be unloaded later but you would have to code that yourself and it imposes restrictions on the types you plan to use in the separate AppDomain. That is, those types need to work through .NET Remoting so they either have to derive from MarshByRefObject or they have to be serializable (and this applies to all the objects referenced by their properties, and so on down the object graph).

like image 36
Keith Hill Avatar answered Sep 21 '22 07:09

Keith Hill