PowerShell : Where is my taskbar ?

If you often work with PowerShell, and especially PowerShell GUI, then sometimes you will need to know :

  • How many computer monitors are connected ?
  • Which is the primary ?
  • What are the screen resolution (for every monitor attached) ?

Well, you can get all information with :

  • System.Windows.Forms.Screen class

It provides theses information. So you just have to use this class and after you can use the AllScreens properties.

Below are some examples :

First, load modules

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Screen]::AllScreens

On my computer, I got this result :

BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1280,Height=1024}
DeviceName   : \\.\DISPLAY1
Primary      : True
WorkingArea  : {X=0,Y=0,Width=1280,Height=984}

Ok, cool you have informations about your computer monitor.

Now you can imagine to do something like this :

$taskbar = [System.Windows.Forms.Screen]::AllScreens

$taskbar | ForEach-Object {

$workingWidth = $taskbar.WorkingArea.Width
$workingHeight = $taskbar.WorkingArea.Height
$boundsWidth = $taskbar.Bounds.Width
$boundsHeight = $taskbar.Bounds.Height
$monitor = $taskbar.DeviceName

    if ($workingWidth -eq $boundsWidth) {
        write-host "Taskbar : Horizontal"
        write-host "Monitor : $monitor"
    } elseif ($workingHeight -eq $boundsHeight) {
        write-host "Taskbar : Vertical"
        write-host "Monitor : $monitor"
    } else {
        write-host "Taskbar : Hidden ?"
    }
}

It will return the position of your taskbar

Taskbar : Horizontal
Monitor : \\.\DISPLAY1

Use this property to find if taskbar is at the bottom of screen

WorkingArea       : {X=0,Y=0

or on top of the screen

WorkingArea       : {X=0,Y=40

To finish, you can set your GUI position with the location property and this class.

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.

Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /homepages/36/d512170128/htdocs/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048