I want call execution for a myScript1.ps1 script inside a second myScript2.ps1 script inside Powershell ISE.
The following code inside MyScript2.ps1, works fine from Powershell Administration, but doesn't work inside PowerShell ISE:
#Call myScript1 from myScript2
invoke-expression -Command .\myScript1.ps1
I obtain the following error when I execute MyScript2.ps1 from PowerShell ISE:
The term '.\myScript1.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Best Answer
In order to find the location of a script, use
Split-Path $MyInvocation.MyCommand.Path
(make sure you use this in the script context).The reason you should use that and not anything else can be illustrated with this example script.
Here are some results.
In PowerShell 3.0 and later you can use the automatic variable
$PSScriptRoot
: