Clean ISE Powershell Environment / Debug Methodology

When writing a powershell script in ISE, I have something similar (which I am running with F5 ):

Function DoSomethingNow { "What am I doing?" } DoSomethingNow 

The problem is that if I rename or delete DoSomethingNow , it is still available in the session. This causes confusion that my script will continue to work, while I expect an error in the absence of a function.

I can only assume that I need to clear the session after each debug round, however, it seems that this is only possible when restarting ISE (therefore restarting the powershell session).

So I ask:

  • Is it possible to clear a session so that DoSomethingNow no longer in scope?
  • What is the “right” way to debug and execute powershell scripts?
  • Maybe I'm not using ISE in the right way?

I would appreciate it being directed in the right direction.

+4
source share
1 answer

You can “remove” a function using Remove-Item and Function PSDrive

Remove-Item Function:\DoSomethingNow

If you add this to the end of your script and delete each function (only in debug situations, hopefully), you will get a new beginning every time. You can do this for aliases and environment variables if you set them.

You can also look at $Error.Clear() to remove any entries in the $Error object, as well as Remove-Variable (you can guess what this does).

+3
source

All Articles