Powershell ISE caching variable values?


I’m running 64bit Powershell ISE on Windows Server 2008r2.

While debugging I set a variable like:

I then make the following change

I step through the script again and even though the debugger steps over the amended line, when I then hover over the $dbName variable it still shows a value of “db1”. The only way I can seem to work around this is to restart that ISE – which is a pain!

Can anyone tell me where I’m going wrong please?

* Update *

I have the following function in a module:

I have a powershell script that imports the moduleL

From the powershell ISE I place a breakpoint on $dbName = “db1” before executing the function by entering “SampleFunction” in the execution window. All is good and the value “db1” is written to the output window.

I then change the function so that $dbName = “db2”. I re-import the module by executing Import-Module -Name “.\BadPsm.psm1” again.

When I execute the function again I hit the breakpoint, step over and can see that $dbName still equals “db1”, “db1” is also written to the output window.

In order to help illustrate I’ve posted a short screencast to youtube here: youtube link


Your video shows that after changing your module you did not unload it before calling import-module again. Calling Import-Module a second time for the same module does nothing since it is already loaded. That is why you see the result of your old function.

The solution is to call Remove-Module before calling Import-Module a second time. Or use Import-Module’s -Force parameter to force reloading.

The reason why exiting PowerShell ISE helps is because then you start fresh. It has nothing to do with PowerShell ISE. You would also see this effect in the PowerShell console.

The reason why others have tried and not able to reproduce what you are seeing is because they didn’t use modules so each time they change the code, that changed code is run.


Powershell ISE caching variable values? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply