Lock your workstation via CMD or Powershell
First from CMD, that is pretty easy:
rundll32.exe user32.dll, LockWorkStation
Now, with PowerShell, it is about the same, we call the user32.dll, just it is a little more work to get that to work right:
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | foreach { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"@
}
Register-NativeMethod "user32.dll" "bool LockWorkStation()"
Add-NativeMethods
[NativeMethods]::LockWorkStation()
Credit for how to call DLLs from PowerShell goes to Danny.