0
votes

Why does my command behaves differently when I run it from the Windows Explorer and from ISE?

I have a simple command from Test1.ps1 (both Test1 and Test2 are in the same folder)

& ".\Test2.ps1"

When I run it from Windows explorer, the script Test2.ps1 is executed. However, when I run it in ISE it doesnt work and I get the following error:

& : Die Benennung ".\Test2.ps1" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang. In Zeile:1 Zeichen:3 +& ".\Test2.ps1"

  • CategoryInfo : ObjectNotFound: (.\Test2.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Sorry for the german: it basically says: ".\Test2.ps1" was not found as a name of a cmdlet, a function, a script file or an executed programm. check the typing of the name or if the path is correct

1
The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell [Core] 6+. The actively developed editor that offers the best PowerShell development experience, across platforms, is Visual Studio Code, combined with its PowerShell extension.mklement0

1 Answers

1
votes

Please check the working directory shown in the script pane. ISE usually starts on your user profile. Either change to the path of your script first.

Set-Location -Path "C:\ScriptLocation\"

Or use the full path when you call the script:

& "C:\ScriptLocation\Test2.ps1"

When you run it from Explorer, PowerShell is starting on the same folder which is why it is able to find the script.