PoshCode Logo PowerShell Code Repository

Quitting Time Clock by Dan In Philly 3 weeks ago (modification of post by Dan In Philly view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6873"></script>download | new post

Demonstrates how to resize a PowerShell Window, use a count-down timer, toggle a keystroke, and automate logging off your computer. I wrote this so I’d have a simple reminder of these things but I ended up running the script at the start of each work day. I’ve also distributed the “toggle scroll lock” part to a LOT of colleagues who got sick of the corporate screen saver GPO. :)

  1. Clear
  2.     $Host.UI.RawUI.WindowTitle = "Quitting Time Clock"
  3.     $Size = $Host.UI.RawUI.WindowSize
  4.     $Size.Width = 30
  5.     $Size.Height = 3
  6.     $Host.UI.RawUI.WindowSize = $Size
  7. Write-Host "What is today's quitting time?"
  8. $qt = Read-Host "(HH:mm:ss)"
  9. If($qt -eq "") {$qt = "16:00:01"} # Default quitting time is 4pm
  10.  
  11. Do {Clear
  12. $tm = Get-Date -Format HH:mm:ss
  13.  
  14. # Every five minutes toggle the Scroll Lock key to prevent screen saver
  15. If($tm.Substring(4,4) -eq "0:00" -or $tm.Substring(4,4) -eq "5:00"){
  16.     $shell = New-Object -com "Wscript.Shell"
  17.     $shell.sendkeys({ScrollLock}{ScrollLock})
  18.     $shell.Dispose}
  19.  
  20. # Countdown timer code
  21.     $ts = New-Timespan $(get-date) $qt
  22.     Write-Host $([string]::Format("Time Remaining: {0:d2}:{1:d2}:{2:d2}",
  23.     $ts.hours, $ts.minutes, $ts.seconds)) -ForegroundColor Cyan
  24.  
  25. # Display current time code
  26.     Write-Host "         " -NoNewline
  27.     Write-Host $tm -ForegroundColor Yellow
  28.     sleep 1 }
  29. Until ($tm -ge $qt)
  30.  
  31. # Start countdown to Logoff - five minutes after 'quitting time'
  32. $LOTime = (Get-Date).AddMinutes(5).ToString("HH:mm:ss")
  33. Do {Clear
  34.     $LOCount = New-TimeSpan $(Get-Date) $LOTime
  35.     Write-Host $([string]::Format("Logoff in: {0:d2}:{1:d2}:{2:d2}",
  36.     $LOCount.Hours, $LOCount.Minutes, $LOCount.Seconds)) -ForegroundColor Red
  37.     Sleep 1}
  38. Until ($LOCount.Minutes -eq 0 -and $LOCount.Seconds -eq 0)
  39. Shutdown /L /F

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me