Powershell script to get screen working area works in Win10 but not Win11

I needed to get the screen working area (not counting the taskbar) for a program that didn’t have access to this information. For this I used the following powershell script:

Add-Type -AssemblyName System.Windows.Forms
$Scr = [System.Windows.Forms.Screen]::AllScreens
$Di = $Scr | Where-Object -FilterScript {$_.DeviceName -match ‘DISPLAY1’}
$Di.WorkingArea.X, $Di.WorkingArea.Y,
$Di.WorkingArea.Width, $Di.WorkingArea.Height, $Di.Bounds.Height |
Out-File -FilePath c:\Users\paul\workingArea.txt -Encoding ASCII

Then I execute this powershell script by running the following .bat file:

Powershell -ExecutionPolicy Bypass c:\Users\paul\workingArea.ps1

With my particular display this creates a file called workingArea.txt with the following contents (5 lines):

108
0
1812
1080
1080

This is exactly the result I expect for my screen size and taskbar location (on the left). This script worked reliably every time … that is until I updated from Windows 10 to Windows 11. Now under Windows 11, it still creates the workingArea.txt file, however this file is empty (size = 0 bytes).

Does anyone know why this script no longer works under Windows 11 and how it might be fixed?
Unfortunately I don’t really know much about how it works since the script was suggested to me by someone on this forum (perhaps about a year ago).

Thanks
~Paul

Paul,
Welcome to the forum. :wave:t3:

I tested your code and it works just as expected on Windows 11. :man_shrugging:t3:

Did you try to debug your code?

Thanks Olaf for trying my script and reporting that it works. I’m not familiar how to debug a powershell script, but I inserted extra lines to send the intermediate variables to debug files and the results were as expected. Then I noticed the original unmodified script was now working. I would be willing to chalk the first failure to human error or some fluke … except that the first person to try my public domain program (that calls this script) with Windows 11 experienced the same kind of failure. So now I’m unsure if the script is going to be reliable enough to use. Perhaps more experience will answer this question. I did notice that the script took a long time to execute. About 4 seconds the first time after booting and about 2 seconds after that. Seems strange that it would take so long. Moving back to a windows 10 computer I noticed that it ran very quickly … perhaps 100 milliseconds the first time and no perceptable delay after that. That was a somewhat faster computer, but I wouldn’t think that would make so much difference. I was hoping to get it to fail again, but I haven’t managed to after repeated trys. Now it always seems to work. I know I haven’t given you much to go on, but if you do have any insights, let me know. Thanks ~Paul

That’s basically it. You observe the states and contents of the variables and either inspects them with a debugger or simply outputs them to the console or log files. :+1:t3: :man_shrugging:t3:

Great. Problem solved, next topic. :man_shrugging:t3: :+1:t3: :smirk:

I didn’t get that … what? :thinking:

It depends a little on how you run the script … but does the runtime really matter? … I mean … really … :smirk:

How important is this code snippet? Does your business rely on it? Are you really willing to spend more time and energy on this little piece of code?

I was just saying that I know of at least one other person who saw this script fail. I don’t see what was confusing about that statement.

And no, the runtime doesn’t matter. Generally the script only needs to run once, and again any time the user moves or resizes the taskbar. But most people rarely if ever move the task bar. I was just mentioning it as a curiosity, although perhaps it might be related to why it might fail.

You seemed incredulous that I was spending time on this little snippet. For me it seemed interesting and useful mostly because the programming environment I was using had no easy way of determining the screen area, sometimes causing figures to overlap the taskbar. Also programming for me is basically a hobby as my livelihood doesn’t depend on it. Since you are likely a professional programmer, perhaps my efforts seem pedestrian. In any case, I appreciate your perspective. I will probably continue to use the script for now and see what happens.

Thanks
~Paul

I’m not a native english speaker. :wink:

No. At the end of the day it is your decision how you spend your time. I just wanted to give some food for thought as to whether it’s really worth trying to figure out why the script runs a little slower on a different computer. :man_shrugging:t3:

I am an IT system engineer. :man_shrugging:t3:

Not at all. But I never needed something like this so far and I think it’s a quite rare requirement. And since your code worked just fine I haven’t had the urge to dive deeper into that topic. :man_shrugging:t3: