Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide title bar in powershell

Tags:

powershell

In a Powershell environment, is it possible to hide the title bar or at least remove the close button?

I have some scripts that I'd prefer the user not "poke" at while they're running. I've considered running the script as hidden, but then the system will look like it's stuck for a minute or completely done when things are actually still going on under the covers.

like image 841
UtopiaLtd Avatar asked Sep 19 '25 02:09

UtopiaLtd


1 Answers

You can disable the close button of the Windows console with this script at poshcode.org. However, the user can still close the console from the taskbar, and it doesn't work on console replacements such as ConEmu.

$code = @'
using System;
using System.Runtime.InteropServices;

namespace CloseButtonToggle {

 internal static class WinAPI {
   [DllImport("kernel32.dll")]
   internal static extern IntPtr GetConsoleWindow();

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DeleteMenu(IntPtr hMenu,
                          uint uPosition, uint uFlags);

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   internal static extern bool DrawMenuBar(IntPtr hWnd);

   [DllImport("user32.dll")]
   internal static extern IntPtr GetSystemMenu(IntPtr hWnd,
              [MarshalAs(UnmanagedType.Bool)]bool bRevert);

   const uint SC_CLOSE     = 0xf060;
   const uint MF_BYCOMMAND = 0;

   internal static void ChangeCurrentState(bool state) {
     IntPtr hMenu = GetSystemMenu(GetConsoleWindow(), state);
     DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
     DrawMenuBar(GetConsoleWindow());
   }
 }

 public static class Status {
   public static void Disable() {
     WinAPI.ChangeCurrentState(false); //its 'true' if need to enable
   }
 }
}
'@

Add-Type $code
[CloseButtonToggle.Status]::Disable()
like image 74
Rynant Avatar answered Sep 22 '25 05:09

Rynant