How to find Computer Screen is Locked or not using Powershell

This topic contains 3 replies, has 3 voices, and was last updated by  Jason Yoder 1 year, 10 months ago.

  • Author
  • #53202

    Amar Helloween

    Hi All,

    I am performing a GUI Based Automation through PowerShell , but the problem I am facing is the script will only run on an interactive session if the machine is locked, it waits to get unlocked. So is there any way to know if the machine is locked and if yes then how to login or make it run, just saying 😛

    Lets have some idea on the this, and see this possibility.

  • #53266

    Dan Potter

    You want the script only to run while locked?

    Look for EventID=4634 in the eventlog

  • #53274

    Dan Potter

    1..10 | % {

    $currentuser = gwmi -Class win32_computersystem | select -ExpandProperty username
    $process = get-process logonui -ea silentlycontinue

    if($currentuser -and $process){"computer is locked"}else{"unlocked"}

    start-sleep 5


  • #53338

    Jason Yoder


    This question came up a few years ago in one of my Server classes. Here is my posted solution. It is a combination of PowerShell and Advanced Auditing.


You must be logged in to reply to this topic.