Using Powershell to detect the status if the laptop lid is open or shut

5 posts / 0 new
Last post
Author
Message
tyrone.stallard...
Posted: 20 November 2019 - 8:42am
Using Powershell to detect the status if the laptop lid is open or shut

Hello all, I searched the WMI classes to try and figure out if there is a way to detect the status of a laptop lid and verify if it's open or shut. The reason is I'm testing a new script to update the bios and if a new version if found compared to what is installed on the machine.

It's working great but I had one user said when her laptop was plugged into a dock and her laptop lid was closed.  She rebooted, which then it should have updated the laptops bios.  Instead, it went to sleep. 

She had to open the laptop lid for the BIOS update to complete.  Her BIOS setting Power On Laptop when lid is open" was "disabled".

So, in the future, I like to find out if there is a Win32 class I can use to check the laptop lid status.  Is it open or shut.

 

Any ideas or maybe some links you can share would be great.

Top
Author
Message
txvalp
Posted: 20 November 2019 - 10:47am
Don't see an obvious way

Unfortunately I don't think WIndows exposes this in a standard way. It is passed from the BIOS to Windos via ACPI, but windows seems to keep it to itself. Unless someone has found an undocumented place where WIndows squirrels away that information.

You may be able to get it via power change events, but that is not easy to accomplish in powershell, and would require something running to receive the events.

(Edit) I added some code about checking for the arrival of the internal monitor as a PNP device, but that didn't work correctly, so I removed it. Apparently once the device appears, it never leaves.

 

 

 

 

Top
Author
Message
tyrone.stallard...
Posted: 20 November 2019 - 11:22am
Docking Station Idea

So what I thought of was the only time a lid is closed while the laptop is in use is to check to see if there is a docking station currently attached.  If it's not plugged into a dock, then the lid must be open.

I tried this cmdlet with the -presentonly switch and it seems to at least show me there is a dock attached.  I guess that is one way I can detect if the lid is shut, unless there is a random person using the dock with his lid open.

 Get-PNPDevice -PresentOnly | Where-Object -Property "Class" -EQ "USBDevice"

Top
Author
Message
txvalp
Posted: 20 November 2019 - 11:45am
That may work

You can trigger off of any device that changes state when the lid is opened or close. IN some cases, a new USB audio may come and go, if a docking station is attached.

 

Top
Author
Message
tyrone.stallard...
Posted: 20 November 2019 - 11:48am
Worked.

This seems to have worked for me, I used the same command and it detected the dock.  When I unplugged it nothing comes up, so if the value is null I can move on and continue the update. If the dock is deteted, I can prompt a windows toast notification to open the laptop lid before rebooting.

Thanks again.

Top
Please login to comment