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,
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
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