Question

I'm writing my PowerShell scripts with Windows PowerShell ISE. When I change someting in the script and run the script, not the last saved version of the script is executed but the older one. Only if I run the script a second time it uses the current version. What can I do to always run the newest version of the script?

Était-ce utile?

La solution

This is a very old question, but I think I've stumbled across the same issue. In my case I've defined a function after invoking it. It appears to work, but only because "myfunc" still has the value from the previous invocation. If you change "Hello, World!", you'll find that the new value takes affect only on the second attempt.

Invoke-Command -ScriptBlock ${function:myfunc}

function myfunc() {
    Write-Host "Hello, World!"
}

To resolve the issue, simply define the function before you attempt to call it.

function myfunc() {
    Write-Host "Hello, World!"
}

Invoke-Command -ScriptBlock ${function:myfunc}

Autres conseils

After making an edit, you need to source the script again by dot sourcing it. Assuming you have a file named MyScript.ps1 in the current directory, in the console you run the command below:

. .\MyScript.ps1

If you want to call a specific function in the script then you can just do:

. .\MyScript.ps1
MyFunction

My experience with ISE "caching" of old files:

The behaviour of ISE is different for PS modules ( .psm1 ) and simple PS scripts ( ps1 ). I am using PS&ISE with Win10Pro.

A) My experience with modules ( . PSM1 )

  1. Load a module file "hello.psm1" already placed in a proper module directory "C:\Users\MyUserName\Documents\WindowsPowerShell\Modules\Hello" by ISE
  2. Execute a function by "Run Selection" ( you may not execute modules by "Run Script" )
  3. Modify the file, e.g. the output of the function 'Write-Host "Hello World!"' to 'Write-Host "Hello"' and save the file.
  4. Execute a function by "Run selection", and it will execute the old function, with old output, e.g. "Hello World!". This is also true if I repeat the "Run Selection" command.
  5. Just if I leave ISE and load it again, the new function is executed by "Run Selection".

B) My experience with scripts ( .PS1 ).

  1. If I execute "Run Selection", the same behaviour as with modules.
  2. If I execute once "Run Script" and by that the function is called, the current version of the function is executed. Of course I call the function ( e.g. on line number 100 ) in the file "after" it is defined ( e.g. in lines 10-20 ). By this, there is no caching.
  3. Especially and consequently, if I execute once "Run Script", while the executed code doesn't call the function, and then execute my function by "Run Selection", the current version of the function is executed.

C) Here is my workaround to make module development more comfortable:

  • Simple scripts may be executed by "Run script".
  • If I want to develop a module ( .PSM1 ), I name it as simple script ( .PS1 ) during the development phase. I place the file ( e.g. "hello.ps1" ) already in the proper module folder, e.g. "C:\Users\MyUserName\Documents\WindowsPowerShell\Modules\Hello" for a module "Hello". Of course by this I can´t execute the functions as module functions from PowerShell console. I just want to use ISE to call functions for testing.
  • As modules are not executed as script, there is no executable code in the file, but just functions ( AFAIK ). So I may safely execute a "Run Script" on my script.

So if I modify a module file under ISE development, I always first execute "Run Script", before I execute a function by "Run Selection".

By this I always execute the current version of a function.

Though my problem was a little bit different, this question and the answers were very helpful for me to find a solution. It is hard to find such questions about ISE "caching" and so helpful answers.

Sincerely Rolf

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top