LockWorkstation using Powershell and pInvoke

[code language=”powershell” title=”Powershell Code”]
$signature = @’
[DllImport("user32.dll", SetLastError = true)]
public static extern bool LockWorkStation();
Add-Type -MemberDefinition $signature -Name CustomName -Namespace CustomNamespace -PassThru


  • Keyword PUBLIC is a must, or else wont be able to call the native function.
  • Unlike variables, Add-Type cant be changed once created in a powershell session.


Leave a comment

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

WordPress Appliance - Powered by TurnKey Linux