开发者

clear powershell session

开发者 https://www.devze.com 2023-01-30 08:17 出处:网络
Is there a commandlet that clears th开发者_StackOverflow中文版e current powershell session variables that I have added?

Is there a commandlet that clears th开发者_StackOverflow中文版e 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."


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消