Locking the Screen with PowerShell

by Apr 4, 2019

Here is a PowerShell function called Lock-Screen that can lock the screen and prohibit user interaction. There can be a custom message, and the screen can be dimmed during lockout.

Here is an example call:

 
PS> Lock-Screen -LockSeconds 4 -DimScreen -Title 'Go away and come back in {0} seconds.'  
 

And this is the source code to run in order to use Lock-Screen:

Function Lock-Screen
{
  [CmdletBinding()]
  param
  (
    # number of seconds to lock
    [int]
    $LockSeconds = 10,

    # message shown. Use {0} to insert remaining seconds
    # do not use {0} for a static message
    [string]
    $Title = 'wait for {0} more seconds...',
    
    # dim screen
    [Switch]
    $DimScreen
  )

  # when run without administrator privileges, the keyboard will not be blocked!

  # get access to API functions that block user input
  # blocking of keyboard input requires admin privileges
  $code = @'
    [DllImport("user32.dll")]
    public static extern int ShowCursor(bool bShow);

    [DllImport("user32.dll")]
    public static extern bool BlockInput(bool fBlockIt);
'@

  $userInput = Add-Type -MemberDefinition $code -Name Blocker -Namespace UserInput -PassThru

  # get access to UI functionality
  Add-Type -AssemblyName PresentationFramework
  Add-Type -AssemblyName PresentationCore
  
  # set window opacity
  $opacity = 1
  if ($DimScreen) { $opacity = 200 }
  
  # create a message label
  $label = New-Object -TypeName Windows.Controls.Label
  $label.FontSize = 60
  $label.FontFamily = 'Consolas'
  $label.FontWeight = 'Bold'
  $label.Background = 'Transparent'
  $label.Foreground = 'Blue'
  $label.VerticalAlignment = 'Center'
  $label.HorizontalAlignment = 'Center'
  
  
  # create a window
  $window = New-Object -TypeName Windows.Window
  $window.WindowStyle = 'None'
  $window.AllowsTransparency = $true
  $color = [Windows.Media.Color]::FromArgb($opacity, 0,0,0)
  $window.Background = [Windows.Media.SolidColorBrush]::new($color)
  $window.Opacity = 0.8
  $window.Left = $window.Top = 0
  $window.WindowState = 'Maximized'
  $window.Topmost = $true
  $window.Content = $label
  
  # block user input
  $null = $userInput::BlockInput($true)
  $null = $userInput::ShowCursor($false)
  
  # show window and display message
  $null = $window.Dispatcher.Invoke{
    $window.Show()
    $LockSeconds..1 | ForEach-Object {
      $label.Content = ($title -f $_)
      $label.Dispatcher.Invoke([Action]{}, 'Background')
      Start-Sleep -Seconds 1
    }
    $window.Close()
  }
  
  # unblock user input
  $null = $userInput::ShowCursor($true)
  $null = $userInput::BlockInput($false)
}

Note that Lock-Screen needs to be run with Administrator privileges in order to completely lock user input.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!