1

I have been working in a script using Powershell ISE for a few months and for some reason, a weird issue started to happen today.

If I edit the script, then save and run it from the same ISE window/session, it seems the changes made has no affect. If I run the code again after close and reopen the ISE, then I see the effect of the changes.

That issue happens for every single change. I have rebooted the system and the issue still. How could it be?

Cleber Marques
  • 426
  • 5
  • 20
  • 3
    when the ISE stays open ... it keeps EVERYTHING that is in the session. so - until your code overwrites the old values - your old values are there and WILL be used. [grin] the fix is to explicitly initialize everything at the start of each run OR to open a new session OR to use the `file/new-powershell-tab` menu command to open a new session inside the current ISE. new PoSh sessions in the ISE are shown by a square tab instead of the usual rounded tabs. – Lee_Dailey May 15 '20 at 22:30
  • When you run a script within ISE, it is like dot sourcing the file. So all the variables become global. – js2010 May 16 '20 at 03:35
  • I don't think this is the issue. I am seeing the same thing. No matter what I do changes are not picked up till after I start a new session, even if, when working on a module, I reimport module. For example, I'm writing a module to help me setup up VMs, I add an option/parameter to one of my commands `Set-VMFirmware -VM $vm -FirstBootDevice $dvdDrive` => `Set-VMFirmware -VM $vm -FirstBootDevice $dvdDrive -SecureBootTemplate MicrosoftUEFICertificateAuthority` yet the template is not changed. How would variables in session affect that? I've verified $dvdDrive and $vm are set properly – CSCoder Oct 13 '21 at 15:31

1 Answers1

0

Check if you have the functions defined in the correct order in your file.

Powershell processes in order (top-down) so the function definition needs to be before the function call:

function email($text){
    #email $text
}

#Do things
foreach{
    email($_)
}

The ISE keeps the functions from the last run in memory, so if you have the function call above the function definition, it will run the version from the previous run.

I had the same problem and stumled upon the solution while searching another topic: Powershell script not recognizing my function

keen4000
  • 3
  • 2