Are you tired of your computer going to sleep or your screen locking during important tasks or presentations? Whether you’re downloading large files, attending long online meetings, or simply need your PC to stay active, our enhanced PowerShell script provides a professional solution that subtly moves your mouse to prevent your computer from entering sleep mode.
Table of Contents
Understanding the StayActive.ps1 Script
The enhanced PowerShell script, StayActive.ps1
, leverages Windows API functions to simulate mouse movements while providing professional-grade features like command-line parameters, verbose logging, and progress indicators. Unlike simple scripts, this version is designed for production use with proper error handling and user feedback.
You can find the complete script on GitHub.
Key Features and Improvements
Our enhanced version includes several professional features:
- Command-line parameters for easy customization
- Built-in help system with detailed usage examples
- Progress indicators showing runtime and movement statistics
- Verbose logging with timestamps for monitoring
- Parameter validation to prevent errors
- Graceful error handling and clean termination
- Professional output formatting with color-coded messages
- Runtime statistics including average intervals and total movements
Breaking Down the Script
1. Advanced Parameter Handling
[CmdletBinding()] param( [Parameter(HelpMessage="Time between movements in seconds (default: 60)")] [int]$MoveInterval = 60, [Parameter(HelpMessage="Number of pixels to move (default: 1)")] [int]$SmallMove = 1, [Parameter(HelpMessage="Show progress indicators in console")] [switch]$ShowProgress, [Parameter(HelpMessage="Show this help message")] [switch]$Help )
The
attribute automatically provides the [CmdletBinding()]
parameter, making the script behave like professional PowerShell cmdlets.-Verbose
2. Enhanced Windows API Integration
Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; public class Mouse { [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT lpPoint); } public struct POINT { public int X; public int Y; } "@ -Language CSharp
Beyond basic mouse movement, the enhanced version can track cursor position for detailed logging.
3. Professional Logging System
function Write-DetailedLog { param([string]$Message) Write-Verbose "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $Message" }
Uses PowerShell’s built-in
for consistent, timestamp-based logging.Write-Verbose
4. Intelligent Movement Logic
The script performs precise movements and tracks them:
# Move the mouse right/down [Mouse]::mouse_event($MOUSEEVENTF_MOVE, $SmallMove, $SmallMove, 0, 0) Start-Sleep -Milliseconds 100 # Move back to original position using proper negative coordinates $negativeMove = [uint32]::MaxValue - $SmallMove + 1 [Mouse]::mouse_event($MOUSEEVENTF_MOVE, $negativeMove, $negativeMove, 0, 0)
Command-Line Parameters
Parameter | Type | Default | Description |
---|---|---|---|
-MoveInterval | Integer | 60 | Time between movements in seconds |
-SmallMove | Integer | 1 | Number of pixels to move (1-10) |
-ShowProgress | Switch | Off | Show progress indicators in console |
-Verbose | Switch | Off | Enable detailed logging with timestamps |
-Help | Switch | Off | Display comprehensive help information |
Usage Examples
Basic Usage
# Default settings (60-second intervals) .\StayActive.ps1 # Show progress indicators .\StayActive.ps1 -ShowProgress # Custom interval with progress .\StayActive.ps1 -MoveInterval 30 -ShowProgress
Advanced Usage
# Verbose logging for monitoring .\StayActive.ps1 -Verbose # Full features for long-running sessions .\StayActive.ps1 -MoveInterval 45 -SmallMove 2 -ShowProgress -Verbose # Quick testing with frequent movements .\StayActive.ps1 -MoveInterval 10 -ShowProgress -Verbose
Getting Help
# Display comprehensive help .\StayActive.ps1 -Help
How to Use the Script
Method 1: Direct Execution
- Save the Script: Copy the enhanced script and save it as
StayActive.ps1
- Right-click Execution: Right-click the file and select “Run with PowerShell”
- Monitor Output: Watch the professional startup banner and progress indicators
Method 2: PowerShell Command Line
- Open PowerShell: Navigate to the script directory
- Execute with Parameters:
.\StayActive.ps1 -ShowProgress -Verbose
- Stop Gracefully: Press
to see termination statisticsCtrl+C
Method 3: Handle Execution Policy
If you encounter execution policy restrictions:
# Check current policy Get-ExecutionPolicy # Set policy for current session Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process # Run the script .\StayActive.ps1 -ShowProgress
Sample Output
Startup Banner
============================================================ StayActive Enhanced Script Started ============================================================ Start Time: 2024-12-07 14:30:15 Move Interval: 60 seconds Move Distance: 1 pixel(s) Show Progress: True Verbose Mode: False Press Ctrl+C to stop the script ============================================================
Progress Indicators
[14:31:15] Move #1 | Runtime: 00:01:00 | Next in: 60 sec [14:32:15] Move #2 | Runtime: 00:02:00 | Next in: 60 sec
Termination Summary
============================================================ StayActive Script Terminated ============================================================ End Time: 2024-12-07 16:45:30 Total Runtime: 02:15:15 Total Movements: 136 Average Interval: 59.8 seconds ============================================================
Use Cases
Professional Environments
- Long Presentations: Keep your screen active during extended presentations or demos
- Remote Work: Prevent disconnection during video conferences or screen sharing
- Automated Processes: Ensure uninterrupted execution of long-running automated tasks
Development and Testing
- Build Processes: Keep systems active during lengthy compilation or deployment processes
- Monitoring: Use verbose logging to monitor system activity patterns
- Testing: Customize intervals and movements for specific testing scenarios
Personal Use
- Large Downloads: Prevent sleep mode during substantial file downloads
- Media Streaming: Keep systems active during long media sessions
- Gaming: Prevent AFK timeouts in certain applications
Best Practices
Security and Compliance
- Check Organizational Policies: Ensure compliance with your company’s IT policies
- Use Responsibly: Only use for legitimate purposes that require system activity
- Monitor Usage: Use verbose logging to track when and how the script runs
Performance Optimization
- Adjust Intervals: Use longer intervals (60+ seconds) for minimal system impact
- Minimal Movement: Stick to 1-pixel movements unless specifically needed
- Monitor Resources: Check system resource usage during extended runs
Troubleshooting
- Use Verbose Mode: Enable
for detailed diagnostic information-Verbose
- Check Permissions: Ensure proper execution policy settings
- Test Parameters: Start with short intervals to verify functionality
Advanced Customization
Custom Intervals for Specific Scenarios
# For presentations (every 2 minutes) .\StayActive.ps1 -MoveInterval 120 -ShowProgress # For downloads (every 5 minutes) .\StayActive.ps1 -MoveInterval 300 # For testing (every 10 seconds) .\StayActive.ps1 -MoveInterval 10 -Verbose
Monitoring Long Sessions
# Full monitoring for extended operations .\StayActive.ps1 -MoveInterval 60 -ShowProgress -Verbose
Conclusion
The enhanced StayActive.ps1 script represents a significant evolution from simple mouse movement utilities. With professional features like command-line parameters, comprehensive logging, progress indicators, and robust error handling, it’s designed for production environments where reliability and monitoring are essential.
Whether you’re a developer managing build processes, a presenter delivering extended demos, or someone who needs reliable system activity simulation, this enhanced script provides the professional-grade functionality you need.
Key advantages of the enhanced version:
- Professional: Command-line interface with proper parameter validation
- Monitorable: Comprehensive logging and progress tracking
- Reliable: Robust error handling and graceful termination
- Flexible: Customizable intervals and movement distances
- Maintainable: Clean code following PowerShell best practices
Feel free to customize the script parameters for your specific needs, and remember to use it responsibly within your organization’s guidelines. The script is available under the GPL-3.0 license on GitHub.
Download the enhanced script: StayActive.ps1 on GitHub