How to keep the console top and active with powershell RRS feed

  • Question

  • Hi Microsoft Scripting Guys !
    In  some case i want to keep the script console  top and active 
    So that i needn't to choose current console by clicking  mouse  for console  interaction
    Here is a  piece of code to set the console top  but  not active 
    I don't know how to declare  the setctive  method and thanks  for your help 

    using System;
    using System.Runtime.InteropServices;
    public static class GetApi {
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags); 
        public static int setTop(IntPtr handle) {return GetApi.SetWindowPos(handle, -1, 0, 0, 0, 0, 1 | 2);}
    Add-Type -TypeDefinition $code
    $note=Get-Process -Id $PID | Select-Object -First 1

    Wednesday, May 20, 2020 2:36 AM

All replies

  • For help using the Windows API you should post in the Windows developer forum.

    Setting top and active are not reliable in modern Windows.  Many Windows can set topmost and the last one will always win.  Console Windows don't behave like GUI Windows and will not cooperate.

    "setactive" will be over-taken by just clicking on any other Window.  The code you posted works to make the current PS the topmost as long as no other window claims topmost.

    What is it that is not happening?


    Wednesday, May 20, 2020 4:52 AM
  • Thanks   jry

    Just  like what you said above ,Setting top and active are not reliable in modern Windows

    it  is just a idea and attempt when the mouse click is not convenient 

    for me it is also  a chance to learn more about to Use PowerShell to Interact with the Windows API

    Wednesday, May 20, 2020 8:32 AM