Simulate-Activity: Simulate user mouse and keyboard input with PowerShell

If you want to avoid or block coming up your screensaver locally or remotely, take this nice script.

It will move your mouse cursor and presses space every minute to simulate user activity. This all happens with the help of Wscript.Shell for sending a key press and System.Windows.Forms.Cursor for moving the cursor for 1 pixel.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Function Simulate-Activity
{
<#
.SYNOPSIS
    Simulates Mouse and Keyboard Activity to avoid Screensaver coming up.

.DESCRIPTION
    Simulates Mouse and Keyboard Activity to avoid Screensaver coming up.

.PARAMETER Minutes
    Commit minutes for simulating activity. If no string given you will be asked.

.PARAMETER Verbose
    Run in Verbose Mode.

.EXAMPLE
    PS C:> Simulate-Activity -Minutes 60

.LINK

Home


.NOTES
    Author:  Sebastian Gräf
    Email:   ps@graef.io
    Date:    September 9, 2017
    PSVer:   3.0/4.0/5.0
#>

	[Cmdletbinding()]
	Param (
		[Parameter(Mandatory = $false)]
		[string]$Minutes
	)

	Begin
	{
		Write-Verbose " [$($MyInvocation.InvocationName)] :: Start Process"
	}

	Process
	{
		Add-Type -AssemblyName System.Windows.Forms
		$shell = New-Object -com "Wscript.Shell"

		$pshost = Get-Host
		$pswindow = $pshost.ui.rawui
		$pswindow.windowtitle = 'Activity-Simulator'

        if(!$minutes)
        {
        $Minutes = Read-Host -Prompt "Enter minutes for simulating activity"
        }

		for ($i = 0; $i -lt $Minutes; $i++)
		{
			cls
			$timeleft = $Minutes - $i
			Write-Host (Get-Date -Format HH:mm:ss) -ForegroundColor Green
			Write-Host 'Time left: ' -NoNewline
			Write-Host "$timeleft" -ForegroundColor Red -NoNewline
			Write-Host ' Minutes'
			$shell.sendkeys(' ')
			for ($j = 0; $j -lt 6; $j++)
			{
				for ($k = 0; $k -lt 10; $k++)
				{
					Write-Progress -Activity 'Simulating activity ..' -PercentComplete ($k * 10) -Status "Please ... don't disturb me."
					Start-Sleep -Seconds 1
				}
			}
			$Pos = [System.Windows.Forms.Cursor]::Position
			$x = ($pos.X % 500) + 1
			$y = ($pos.Y % 500) + 1
			[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
		}
	}
	End
	{
		Write-Verbose " [$($MyInvocation.InvocationName)] :: End Process"
	}
}

If you don’t commit a string for the Minutes parameter it will ask you how many minutes you want to simulate activity:

/simulate-activity-simulate-user-mouse-and-keyboard-input-with-powershell/2021-08-10-11-39-27.png
.

After that the activity is running for the given amount of time:

/simulate-activity-simulate-user-mouse-and-keyboard-input-with-powershell/featured-image-preview.png
.