Powershell Script To Start A Random Screen Saver

I have decided to start posting my PoSH scripts as I finish them.
I have just made one that seems to be unique. I wrote a script that will start a screen saver. It can either try to detect the current users settings from the registry, or it can search for any available screen saver, apply filters, and execute one randomly.
The script itself doesn’t do much work, but with a little work on the user behalf it could help in creating a screen saver rotation application.

file: Start-ScreenSaver.ps1

# Start-ScreenSaver
# Version: 1.1

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 PanelDesktop’).{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)
}

~ by lunaticexperimentalist on December 7, 2006.

One Response to “Powershell Script To Start A Random Screen Saver”

  1. Very cool.  You taught me something today! 
     
    http://blogs.msdn.com/powershell/archive/2006/12/07/start-screensaver.aspx
     
    Jeffrey Snover [MSFT]Windows PowerShell/MMC ArchitectVisit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a reply to Jeffrey Cancel reply