Search code examples
powershellmonitor

PowerShell calculate pixel height of Start Bar


I want to place windows on my screen at various positions. I can calculate the dimensions of the screen as below (these are arrays if have multi-display setups):

$widths = (Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth).ScreenWidth
$heights = (Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenHeight).ScreenHeight

To properly place windows, I need to also calculate the height of the Start bar. Does anyone know how to programmatically collect the Start bar height using PowerShell?

I would also like to know how to caculate the position of the Start bar (so that I know if it is placed at the top, bottom, left, right of the screen) if possible?


Solution

  • Instead of using WMI, you should be safe using the $Screen.WorkingArea you can retrieve using for instance [System.Windows.Forms.Screen]::PrimaryScreen.

    Having said that, the function below will get you the dimensions and position of the TaskBar for a certain screen:

    Add-Type -AssemblyName System.Windows.Forms
    
    function Get-TaskBarDimensions {
        param (
            [System.Windows.Forms.Screen]$Screen = [System.Windows.Forms.Screen]::PrimaryScreen
        )        
    
        $device = ($Screen.DeviceName -split '\\')[-1]
        if ($Screen.Primary) { $device += ' (Primary Screen)' }
    
        if ($Screen.Bounds.Equals($Screen.WorkingArea)) {
            Write-Warning "Taskbar is hidden on device $device or moved to another screen."
            return
        }
    
    
        # calculate heights and widths for the possible positions (left, top, right and bottom)
        $ScreenRect  = $Screen.Bounds
        $workingArea = $Screen.WorkingArea
        $left        = [Math]::Abs([Math]::Abs($ScreenRect.Left) - [Math]::Abs($WorkingArea.Left))
        $top         = [Math]::Abs([Math]::Abs($ScreenRect.Top) - [Math]::Abs($workingArea.Top))
        $right       = ($ScreenRect.Width - $left) - $workingArea.Width
        $bottom      = ($ScreenRect.Height - $top) - $workingArea.Height
    
        if ($bottom -gt 0) {
            # TaskBar is docked to the bottom
            return [PsCustomObject]@{
                X        = $workingArea.Left
                Y        = $workingArea.Bottom
                Width    = $workingArea.Width
                Height   = $bottom
                Position = 'Bottom'
                Device   = $device
            }
        }
        if ($left -gt 0) {
            # TaskBar is docked to the left
            return [PsCustomObject]@{
                X        = $ScreenRect.Left
                Y        = $ScreenRect.Top
                Width    = $left
                Height   = $ScreenRect.Height
                Position = 'Left'
                Device   = $device
            }
        }
        if ($top -gt 0) {
            # TaskBar is docked to the top
            return [PsCustomObject]@{
                X        = $workingArea.Left
                Y        = $ScreenRect.Top
                Width    = $workingArea.Width
                Height   = $top
                Position = 'Top'
                Device   = $device
            }
        }
        if ($right -gt 0) {
            # TaskBar is docked to the right
            return [PsCustomObject]@{
                X        = $workingArea.Right
                Y        = $ScreenRect.Top
                Width    = $right
                Height   = $ScreenRect.Height
                Position = 'Right'
                Device   = $device
            }
        }
    }
    

    To get the TaskBar dimensions for the Primary screen only:

    Get-TaskBarDimensions
    

    To get the TaskBar dimensions for all connected screens:

    [System.Windows.Forms.Screen]::AllScreens | ForEach-Object {
        Get-TaskBarDimensions $_
    }
    

    This will return an object with the following properties:

    X        : 0
    Y        : 1160
    Width    : 1920
    Height   : 40
    Position : Bottom
    Device   : DISPLAY1 (Primary Screen)
    

    Or -in case the taskbar is hidden or not present on that screen- a warning like:

    Taskbar is hidden on device DISPLAY2 or moved to another screen.