Reputation: 345
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?
Upvotes: 12
Views: 4746
Reputation: 101
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 )
B) My experience with scripts ( .PS1 ).
C) Here is my workaround to make module development more comfortable:
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
Upvotes: 0
Reputation: 3505
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}
Upvotes: 9
Reputation: 2853
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
Upvotes: 1