Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

POWERSHELL FUN SEND KEYS ON THE SCREEN

December 29, 2016 08:34PM

Today I received weird request from one of my developer friend, He created some application but whenever screen was getting locked due to screen saver lock Group Policy after designated time, Application was not working as expected, Until he resolve the bugs in his application he was seeking help if there is any possibility that server won't get automatically locked due to screen saver policy, And I also need not to make any changes to group policy for single this single PC.

For this below is the small PowerShell Script, Which sends keyboard key NUMLOCK twice on the screen after every 60 seconds, which doesn't allow to lock the screen.

To run this script, copy it to notepad and save it as .PS1 extension using double quotes, 

save powershell ps1 file format extension from notepad with double quote

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$Seconds = 60

$ObjShell =  New-Object -ComObject Wscript.Shell
$i=0
do
{
    Write-Host " " -BackgroundColor (Get-Random 'Black', 'DarkBlue','DarkGreen','DarkCyan', 'DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White') -NoNewline
    $ObjResult = $ObjShell.SendKeys("{NUMLOCK}{NUMLOCK}")
    Start-Sleep -Seconds $Seconds
}
while ($x -lt 0)

Once saved it in ps1 file. Run it using cmd with below one liner command.

PowerShell -NoProfile -File c:\temp\nolock.ps1 -ExecutionPolicy Unrestricted

run powershell from cmd, powershell -noprofile -file nolock.ps1 -executionpolicy unrestricted

I have added one more code for fun once run below code on the screen it shows some cool random colors on the screen. Same is in the script.

1..300 | foreach {Write-Host " " -BackgroundColor (Get-Random 'Black', 'DarkBlue','DarkGreen','DarkCyan', 'DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White') -NoNewline}

#Revised this code after got help on facebook.
1..300 | foreach {Write-Host " " -BackgroundColor ([ConsoleColor].GetEnumValues() | Get-Random) -NoNewline

vcloud-lab.com powershell fun with color code on the screens, fun friday

Go Back

Comment