Skip to content

Commit ed2f799

Browse files
author
East
committed
Script added
0 parents  commit ed2f799

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

7.65 KB

🕒 WorkTimer

WorkTimer is a simple PowerShell script that enforce limited sessions. It runs a 1-hour countdown timer, gives spoken and visual notifications before time runs out, and logs out the user automatically. The script is designed to be run at user login and supports Cyrillic output.


🔧 Features

  • ✅ 1-hour countdown (default)
  • 🔔 Visual + spoken alerts at customizable time thresholds
  • 🧪 Test mode for fast debugging (15s countdown)
  • 🚪 Automatic forced logoff when time is up
  • 🖥️ Fully compatible with Windows 11, using only built-in tools (PowerShell + VBScript)

📦 Files Included

WorkTimer.ps1     # Main PowerShell script with alert + logout logic
WorkTimer.vbs     # Helper VBScript to run the PowerShell script silently
README.md         # This documentation

▶️ Usage

✅ Normal mode

To start a 1-hour session:

powershell.exe -NoProfile -ExecutionPolicy Bypass -File WorkTimer.ps1

This uses default alert thresholds: 10, 5, and 1 minute remaining.

🧪 Test mode

Quick 15s run for testing (alerts at 10s, 5s, and 1s):

powershell.exe -NoProfile -ExecutionPolicy Bypass -File WorkTimer.ps1 -Test -Verbose

In test mode:

  • Shortened countdown
  • Verbose output
  • No logout

Quck 15s run for testing with logout:

powersherll.exe -NoProfile -ExecutionPolicy Bypass -File WorkTimer.ps1 -Test -Shutdown -Verbose

🎛️ Custom thresholds

powershell.exe -File WorkTimer.ps1 -Thresholds @(900, 300, 60)

This triggers alerts at 15, 5, and 1 minute remaining.


🗣️ Notification Behavior

Each alert:

  • Shows a balloon tip via Windows Forms
  • Speaks a message using SAPI (text-to-speech)

The script supports Unicode messages (e.g. Cyrillic).


🚀 Auto-Start on User Login

To configure the script to run at user login using Task Scheduler:

  1. Copy WorkTimer.ps1 and WorkTimer.vbs to a foler, e.g. C:\Scripts\
  2. Press Win + R, type taskschd.msc
  3. Click Create Task...
  4. On the General tab:
  • Name: WorkTimer
  • Description: Logout user after 1 hour
  • When running the task, use the following user account:
    1. Click Change User or Group
    2. In Enter the object name to select (examples): type user name of user, which u want it script to work.
    3. Press Check Names
    4. Press OK
  1. On the Triggers tab:
  • Click New...
  • Begin the task: At log on
  • Select Specific user:
  • Choose user account as you did before
  • Click OK
  1. On the Actions tab:
  • Click New...
  • Action: Start a program
  • Program/script: wscript.exe
  • Add arguments: C:\Scripts\WorkTimer.vbs
  • Click OK
  1. Click OK to save the task

This setup ensures the script runs silently via the .vbs wrapper each time user logs in.


⚠️ Permissions & Execution Policy

You may need to allow script execution:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

If the script was downloaded:

Unblock-File -Path WorkTimer.ps1

Or just bypass execution policy for this script:

powershell.exe -ExecutionPolicy Bypass -File WorkTimer.ps1

🧰 Advanced: VBScript Wrapper (WorkTimer.vbs)

The included WorkTimer.vbs allows you to launch the PowerShell script silently (no terminal window):

Set shell = CreateObject("WScript.Shell")
shell.Run "powershell -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\WorkTimer.ps1", 0

Useful for auto-start without interrupting the user.


💡 This is not high level all cases prepared script. Just thing I use by myself.


📜 License

MIT License. Use it, tweak it, improve it.

WorkTimer.ps1

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -------------------------
2+
# WorkTimer.ps1
3+
# Purpose: 1-hour countdown + alerts + auto-logoff
4+
# -------------------------
5+
6+
[CmdletBinding()]
7+
8+
param(
9+
[switch] $Test,
10+
[switch] $Shutdown,
11+
[int[]] $Thresholds = @(600,300,60)
12+
)
13+
14+
# --- Helper: show balloon tip + speak text ---
15+
function Show-Alert {
16+
param(
17+
[string]$Text,
18+
[int]$DurationMs = 5000
19+
)
20+
21+
Write-Verbose "Preparing balloon tip: '$Text'"
22+
23+
# Load WinForms for ballon tip
24+
25+
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
26+
27+
# Create Notification
28+
$ni = New-Object System.Windows.Forms.NotifyIcon
29+
$ni.Icon = [System.Drawing.SystemIcons]::Information
30+
$ni.BalloonTipTitle = 'Session Timer'
31+
$ni.BalloonTipText = $Text
32+
$ni.Visible = $true
33+
34+
# Show it
35+
$ni.ShowBalloonTip($DurationMs)
36+
37+
# Speak via SAPI
38+
$voice = New-Object -ComObject SAPI.SpVoice
39+
$voice.Speak($Text) | Out-Null
40+
41+
Write-Verbose "Spoken via SAPI.SpVoice"
42+
43+
# Clean up after a bit
44+
Start-Sleep -Milliseconds ($DurationMs + 500)
45+
$ni.Dispose()
46+
}
47+
48+
if ($Test) {
49+
# 10s, 5s, 1s instead of minutes
50+
$Thresholds = @(10,5,1)
51+
Write-Host '[TEST MODE] Using thresholds:' $Thresholds
52+
}
53+
54+
# --- Main countdown logic ---
55+
$totalSec = if ($Test) { 15 } else { 4200 }
56+
57+
# Track remaining time
58+
$prev = $totalSec
59+
foreach ($t in $thresholds) {
60+
# Sleep until we hit this threshold
61+
Write-Verbose "Sleeping for $($prev - $t) seconds until next threshold"
62+
63+
try {
64+
Start-Sleep -Seconds ($prev - $t)
65+
}
66+
catch {
67+
Write-Error "Sleep failed: $_"
68+
break
69+
}
70+
71+
if ($Test) {
72+
Show-Alert -Text ("До выключения осталось {0} секунд{1}!" -f ($t), $( if ($t -le 2) { 'а' } else { '' } ))
73+
}
74+
else {
75+
Show-Alert -Text ("До выключения осталось {0} минут{1}!" -f ($t/60), $( if ($t -le 65) { 'а' } else { '' } ))
76+
}
77+
78+
$prev = $t
79+
}
80+
81+
# Final sleep until zero
82+
Start-Sleep -Seconds $prev
83+
84+
# Time's up-force logoff
85+
Show-Alert -Text 'Время вышло, выключение компьютера...' -DurationMs 3000
86+
87+
if (!$Test -or $Shutdown) {
88+
Start-Process -FilePath shutdown.exe -ArgumentList '/l','/f' -NoNewWindow
89+
}

WorkTimer.vbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Set oShell = CreateObject("WScript.Shell")
2+
oShell.Run "powershell.exe -STA -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File ""C:\Scripts\WorkTimer.ps1""", 0, False

0 commit comments

Comments
 (0)