Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Last active February 28, 2023 17:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/c61465ed897c2f10e9bf16704d1d9af9 to your computer and use it in GitHub Desktop.
Save RichardBronosky/c61465ed897c2f10e9bf16704d1d9af9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# From: https://gist.github.com/RichardBronosky/c61465ed897c2f10e9bf16704d1d9af9
# This works from both WSL/bash and powershell!
powershell.exe -command "& (Get-ItemProperty 'HKCU:Control Panel\Desktop').{SCRNSAVE.EXE}"
# Start-ScreenSaver
# Version: 1.1
# From: https://lunaticexperiments.wordpress.com/2006/12/07/powershell-script-to-start-a-random-screen-saver/
param ([String]$Include, [String]$Exclude)
#Avoid execution if a screen saver is already running
if (get-Process *.scr) {
return
}
#If filters are not provided default to user preference.
if ( -not $Include -and -not $Exclude) {
[String]$DefaultScreenSaver = (Get-ItemProperty 'HKCU:Control Panel\\Desktop').{SCRNSAVE.EXE}
if ($DefaultScreenSaver) {
& $DefaultScreenSaver
return
}
}
#Get a list of available screen savers and filter them.
[System.Management.Automation.ApplicationInfo[]]$ScreenSaverCommands = Get-Command *.scr -CommandType Application |
Where-Object {
$(
if ($Include) { $_.Name -like $Include -or $_.Name -eq $Include + '.scr' }
else {$true}
) -and
$(
if ($Exclude) { -not ( $_.Name -like $Exclude ) }
else {$true}
)
}
#Randomly choose a screen saver and execute it.
if ($ScreenSaverCommands) {
& $(($ScreenSaverCommands[$(New-Object System.Random).Next($ScreenSaverCommands.Length)]).Definition)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment