Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Last active August 26, 2020 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexalon/b535d1afa578e9c476ab to your computer and use it in GitHub Desktop.
Save Hexalon/b535d1afa578e9c476ab to your computer and use it in GitHub Desktop.
Uses C# to call an Win32 API to lock the computer
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.117
Created on: 3/25/2016 17:33
Created by: Colin Squier <hexalon@gmail.com>
Filename: Lock-Computer.ps1
===========================================================================
.DESCRIPTION
Uses C# to call an Win32 API to lock the computer
#>
$code = @"
using System;
using System.Runtime.InteropServices;
public static class LockDesktop
{
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "LockWorkStation")]
private static extern IntPtr LockWorkStation();
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
public static void SetScreenSaverRunning()
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
LockWorkStation();
}
}
"@
Add-Type -TypeDefinition $code
[LockDesktop]::SetScreenSaverRunning()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment