PowerShell Script to Keep Screen Active

PowerShell script to keep screen active

Using while Loop with SendKeys() Method

Use the while loop to invoke the SendKeys() method for an unlimited time to keep the PC/Laptop screen active using PowerShell.

Here, we used the New-Object cmdlet to create an instance of the WSH (Windows Script Host) Shell object using COM (Component Object Model) technology and stored it in $wScriptShellObj. This object lets the script interact with the Windows shell and run different shell commands.

Then, we entered an infinite loop using while(1) that would keep it running until the script was terminated using Ctrl+C. Inside the loop, we used the SendKeys() method of the $wScriptShellObj object, which sent the . to an active application once every 2 seconds. If we opened NotePad, the script entered dots there, and if we opened NotePad++, it printed dots there.

The SendKeys() method simulated the keystroke as if it was typed/entered by the user. After each keystroke, the script paused for 2 seconds using the Sleep command and then continued with the next iteration of the while loop. This way, we can keep our PC screen active.

Using while Loop with SendWait() Method

Use the while loop to call the SendWait() method for an unlimited time to keep the PC/Laptop screen active using PowerShell. Press Ctrl+C to terminate the script.

Again, we used a while loop that would run unlimitedly because the condition $true would always be True. Inside the while loop, we used the LoadWithPartialName() method of the System.Reflection.Assembly class. Remember that the [System.Reflection.Assembly] referred to the System.Reflection.Assembly class in .NET, which provided properties and methods to work with assemblies.

We used the LoadWithPartialName() method by specifying the 'System.Windows.Forms' as an argument to load the System.Windows.Forms assembly into the current PowerShell session. The [void] cast was used to discard any value or output of the LoadWithPartialName() method call. In addition, it was used to suppress any value produced while loading the assembly.

Why did we load the System.Windows.Forms assembly? Loading was mandatory to ensure that the System.Windows.Forms.SendKeys class would be available for use and provide methods to simulate the keystrokes.

Now, we used the SendWait() method of System.Windows.Forms.SendKeys class to simulate hitting the NumLock key. Here, the "{NUMLOCK}" string was passed as an argument to the SendWait() method; this string argument specified the keystroke sequence to be sent.

After that, we used the Start-Sleep cmdlet to sleep for two seconds. This cmdlet ensured that the statements within the loop would be executed every two seconds. This way, we can prevent our PC/Laptop from entering sleep mode or screen lock via simulating keystrokes and sending them to an active window or application.

Using keybd_event() Method

Use the keybd_event() method to keep the PC/Laptop screen active unlimitedly using PowerShell. Press Ctrl+C to stop this script.

First, we defined a PInvoke signature for keybd_event() from the user32.dll in C#. This signature was stored in the $pInvokeSignature variable, which allowed invoking the keybd_event() function from the PowerShell using the Add-Type cmdlet. Let’s break down the signature to understand it before diving into the Add-Type and onwards.

To define a signature, We used the @''@ construct, known as verbatim string literal, to define a multiline string without interpreting escape sequences or escaping special characters. The @' and '@ was used to start and end the verbatim string in PowerShell. Everything between them was taken as a literal string, including the special characters and line breaks.

Next, the [DllImport("user32.dll")] specified that the keybd_event() method would be imported from the user32.dll library. After that, we declared the keybd_event() method with its signature. Following is a brief explanation of the different components of the keybd_event() function declaration:

  • public specified that function can be accessed outside the class.
  • static indicates that the function is a static method of the class, accessible without instantiating the class.
  • extern denoted that the function was implemented externally; in the above example, it was implemented in the user32.dll library.
  • void was the return type of the function showing that keybd_event() would not return any value.
  • byte bVk was a byte type parameter representing a virtual-key code of the given key that we wanted to simulate.
  • byte bScan was a byte type parameter showing the hardware scan code of a key that must be simulated.
  • uint dwFlags was of type unit (unsigned integer), denoting the additional flags to control the key event.
  • uint dwExtraInfo was also of type uint (unsigned integer), used for extra information associated with the keystrokes.

We used the Add-Typecmdlet to add the custom C# type (KeyboardFunctions) to the current PowerShell session. This type was defined in the Wind32Functions namespace and had the keybd_event() function’s PInvoke declaration. We used various parameters with the Add-Type cmdlet:

  • We used the -Name parameter to mention custom C# type.
  • The -Namespace parameter was used to specify the namespace’s name.
  • The -MemberDefinition parameter was used to write the PInvoke signature variable ($pInvokeSignature).

After that, we used the while loop, which would always be true due to specifying the $true condition. Inside the loop, we invoked the keybd_event() method and passed four arguments which are explained below:

  1. 0x90 denoted the virtual-key code for the NUMLOCK key; you can find more virtual codes here.
  2. 0 was the hardware scan code.
  3. 0 indicated the KEYEVENTF_KEYDOWN flag, which means the key was pressed.
  4. The fourth argument was 0 because we didn’t want additional information about keystrokes.

We again invoked the keybd_event() method with 0x90, 0, 2, and 0 as first, second, third, and fourth arguments. All the same as the first call of the keybd_event() function, excluding the third argument, which was 2, denoting the KEYEVENTF_KEYUP flag, means the key was released.

Then, we used the Start-Sleep cmdlet to pause the script execution for 2 seconds, which we specified using the -Seconds parameter. This way, we simulated the pressing & releasing of the NUMLOCK key every two seconds for unlimited time to prevent the PC from going into sleep mode.

The keybd_event() function is available but superseded in newer versions of Windows, so it is recommended to use alternatives; for instance, SendKeys() and SendWait() methods that we learned earlier in this article.

That’s all about powerShell script to keep screen active.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *