Getting different result when running from Power Shell ISE versus directory

Hello,

I am new to Power Shell , and I have a question regarding running power shell script from power shell ISE versus running it from a my directory such as I double click on my power shell script file.

When I ran power shell script from ISE, it gave me my desired result such as a pop up window as I instructed it on my script.

However when I double clicked my power shell script from my directory, the window was appeared very fast in a split second, and gone. I wonder where does it go ? and why it does not give me a window like I was sunning it from Power Shell ISE.

I wonder what do I need to do and how to get the same behavior as I run my script from my directory. So please help me if you could.

 

Many Thanks,

 

Attached is my simple script

using System.Text;

public class Win32 {

[DllImport(“user32.dll”, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport(“user32.dll”, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);

}

public struct RECT

{

public int Left; // x position of upper-left corner

public int Top; // y position of upper-left corner

public int Right; // x position of lower-right corner

public int Bottom; // y position of lower-right corner

}

"@

#Win32::GetWindowRect

$Handle = (Get-Process -Id $Pid).MainWindowHandle

$Rectangle = New-Object RECT

Win32::MoveWindow($Handle, 200, 150, 300, 400, $true)

 

 

Powershell scripts (PS1) are not associated with Powershell.exe, so it’s not designed to double-click to execute the script. If you double-click a script, it should open in Notepad if the associations are default. Open Powershell.exe, drag the file into the prompt and press enter to execute it.

It’s important to know that PowerShell ISE is not actually the same execution environment as PowerShell itself. While PowerShell gives you a console interface, PowerShell ISE is a normal Windows application that includes a PowerShell emulator, which means that it doesn’t interact with the operating system on the same level as the PowerShell console. Basically, command execution in ISE is fake - or at least, it has a few extra layers of abstraction between it and the operating system. The PowerShell team wrote up a list of differences, which is dated now but gives you an idea of how they don’t necessarily match up.

You should also be aware that PowerShell ISE is no longer being actively developed and doesn’t officially support PowerShell 6.

So, the ISE tool is nice for context highlighting and for the context completion features while writing your script, but it’s not a good test environment. If you run into problems running your script in ISE, make sure you test it in the PowerShell console before you put in too much troubleshooting effort. Especially as your scripts get more complex, you should expect ISE to not execute them properly.