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!

  • Alternative Lock-Workstation

    function Lock-Workstation {

    c:\windows\system32\rundl32.exe user32.dll, LockWorkStation}

    Be carefull with LockWorkStation it is case sensitive.  If you get "Error in user32.dll Missing entry: ..." check your case.