Locking Workstation

by Feb 15, 2012

If you ever feel the need to lock your interactive session via PowerShell, here’s a function that can do this (and also illustrates how to use low-level API functionality):

function Lock-WorkStation {
 $signature = @"
 [DllImport("user32.dll", SetLastError = true)]
 public static extern bool LockWorkStation();
 $LockWorkStation = Add-Type -MemberDefinition $signature -Name Win32LockWorkStation -Namespace Win32Functions -PassThru
 $LockWorkStation::LockWorkStation() | Out-Null

Twitter This Tip! ReTweet this Tip!