Logon Script to automatically log off users after a specified timeframe with a warning beforehand

Using this script as a PowerShell Logon Script (via GPO / GPEDIT), you can force a logoff at after a specified timeframe and give a warning to the user X minutes beforehand. You’ll want to force hide the script window using ‘-windowstyle hidden’, otherwise they could just kill it and never get logged off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Logon Script that will auto-logoff a user after a specified amount of time has passed,
# and will give a warning of the impending logoff at a specified amount of time beforehand.
# The Warning Message has 10 second timeout so that the user can't postpone the logoff forever.
$maxTime = 8 # hours
$warnTime = 5 # minutes
$msgTimeout = 10 # seconds
$WarningMSG = "NOTICE! You will be logged off automatically in $warnTime minutes. Please save your files."
# ========================================================================================================
# Calculate Time To Sleep
$sleepyTime = $maxTime * 60 * 60
$warningTime = $warnTime * 60
Sleep -Seconds ($sleepyTime - $warningTime)
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup($WarningMSG,$msgTimeout,"Automatic Logoff Incoming...",0x0)
Sleep -Seconds $warningTime
# ADD LOGOFF CODE BELOW!
Logoff

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.