Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set powershell opacity through commandline

New to powershell & .NET.

It is possible to configure powershell console colors.

However how can i set the opacity of the window. It seems that the InternalHostRawUserInterface class's property takes an enum ConsoleColor.

Is it possible to set the windows transparency?

like image 770
hba Avatar asked Oct 21 '25 13:10

hba


2 Answers

As Joey mentioned in his comment, you'll have to talk to a low-level API to modify the transparency of the window.

Using this example, we can adapt it to PowerShell like so:

function Set-ConsoleOpacity
{
    param(
        [ValidateRange(10,100)]
        [int]$Opacity
    )

    # Check if pinvoke type already exists, if not import the relevant functions
    try {
        $Win32Type = [Win32.WindowLayer]
    } catch {
        $Win32Type = Add-Type -MemberDefinition @'
            [DllImport("user32.dll")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

            [DllImport("user32.dll")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

            [DllImport("user32.dll")]
            public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
'@ -Name WindowLayer -Namespace Win32 -PassThru
    }

    # Calculate opacity value (0-255)
    $OpacityValue = [int]($Opacity * 2.56) - 1

    # Grab the host windows handle
    $ThisProcess = Get-Process -Id $PID
    $WindowHandle = $ThisProcess.MainWindowHandle

    # "Constants"
    $GwlExStyle  = -20;
    $WsExLayered = 0x80000;
    $LwaAlpha    = 0x2;

    if($Win32Type::GetWindowLong($WindowHandle,-20) -band $WsExLayered -ne $WsExLayered){
        # If Window isn't already marked "Layered", make it so
        [void]$Win32Type::SetWindowLong($WindowHandle,$GwlExStyle,$Win32Type::GetWindowLong($WindowHandle,$GwlExStyle) -bxor $WsExLayered)
    }

    # Set transparency
    [void]$Win32Type::SetLayeredWindowAttributes($WindowHandle,0,$OpacityValue,$LwaAlpha)
}

And then use it like:

Set-ConsoleOpacity -Opacity 50

Which will then set the opacity of the window to 50%

like image 165
Mathias R. Jessen Avatar answered Oct 23 '25 04:10

Mathias R. Jessen


hold down control and shift, then move scroll wheel. This might be useless answer, I'm not sure if this is a permanent setting

like image 29
Shawn Pagels Avatar answered Oct 23 '25 04:10

Shawn Pagels