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.