Skip to content

Instantly share code, notes, and snippets.

@au5ton
Last active August 29, 2022 02:58
Show Gist options
  • Save au5ton/ccf615d8a0022538a3714ecb5a10990f to your computer and use it in GitHub Desktop.
Save au5ton/ccf615d8a0022538a3714ecb5a10990f to your computer and use it in GitHub Desktop.
Sets the wallpaper of the current logged in user when ran to 2 different images based on a conditional
#
# Austin Jackson
# Script from:
# Intended for: MD Anderson
#
# Use: Sets the wallpaper of the current logged in user when ran to 2 different
# images, depending on if the C drive capacity is over a threshold (in bytes)
# Tested on Windows 10.
#
# Faults: Windows can be inconsistent about when to refresh the wallpaper if this
# script is run multiple times. I doubt that will be important if this only
# needs to run once to work, but keep that in mind anyway.
#
# function from: https://social.technet.microsoft.com/Forums/windows/en-US/72a9b4bf-071b-47cd-877d-0c0629a9eb90/how-change-the-wallpaperbackground-with-a-command-line-?forum=w7itproui
Function Set-WallPaper($Value)
{
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name WallPaper -value $value
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
}
# Example syntax
# Set-WallPaper -value "C:\Retrowave.png"
$threshold = 270582939648 # 270582939648 bytes roughly equals 252GB
$disk_c = Get-PSDrive C | Select-Object Used,Free
# optionally use the boot drive instead of hard-coding the C drive
$disk_boot = Get-PSDrive (Get-WmiObject Win32_OperatingSystem).SystemDrive.SubString(0,1) | Select-Object Used,Free
echo 'capacity:'($disk_c.Used + $disk_c.Free)
echo 'threshold:'$threshold
If (($disk_c.Used + $disk_c.Free) -gt $threshold) {
echo 'should use HDD picture'
Set-WallPaper -value "C:\hdd.bmp"
} Else {
echo 'should use SSD picture'
Set-WallPaper -value "C:\ssd.bmp"
}
#
# Austin Jackson
# Script from: https://gist.github.com/au5ton/ccf615d8a0022538a3714ecb5a10990f
# Intended for: MD Anderson
#
# Use: Sets the wallpaper of the current logged in user when ran to 2 different
# images, depending on if the C drive capacity is over a threshold (in bytes)
# Tested on Windows 10.
#
# Faults: Windows can be inconsistent about when to refresh the wallpaper if this
# script is run multiple times. I doubt that will be important if this only
# needs to run once to work, but keep that in mind anyway. Changes will take
# effect next time the machine is restarted or if explorer.exe is restarted.
# (Restarting explorer.exe is bad, don't do it. I was being theoretical.
# Reboot the dang box.)
#
# function from: https://social.technet.microsoft.com/Forums/windows/en-US/72a9b4bf-071b-47cd-877d-0c0629a9eb90/how-change-the-wallpaperbackground-with-a-command-line-?forum=w7itproui
Function Set-WallPaper($Value)
{
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name WallPaper -value $value
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
}
# Example syntax
# Set-WallPaper -value "C:\Retrowave.bmp"
# it helps if you use a BMP image because Windows is weirdly fond of them
# If you want to do this by the serial number of the drive instead, here's
# an alternative (I'm new to PowerShell, so bear with me.):
#
# stolen from: https://www.reddit.com/r/PowerShell/comments/35mq24/how_to_get_hard_drive_serial_number_remotely_with/cr7oj78/
#
# NOTE: The following code just shows how to retrieve such information.
# How to use the retrieved information hasn't been implemented
# into this script's logic. These are just stored into variables
# and aren't even printed out to see. This is a HEAD START to a
# full-fledged script that's configured for the intended environment.
#
$disks = Get-WmiObject win32_diskdrive | select model,serialnumber
$temp = $disk[0].SerialNumber | Out-String
$serial_number_string = $temp.trim()
# the previous code is under the assumption that the first drive that shows up
# in the $disks list is the boot drive, which isn't always the case!
# On my Windows 10 desktop, my boot HDD was the first and a flash
# drive was the second. Typing the following command into an
# empty PowerShell window will help list all the values available
# from a diagnostic perspective. I recommend taking a look in there.
# After a couple seconds of looking, it looks like the InterfaceType
# or MediaType properties might be worth looking into.
#
# If you're familiar with Object Oriented Programming, rewriting
# this code to suit your needs shouldn't be too much of an issue.
#
# Get-WmiObject win32_diskdrive | select *
$threshold = 270582939648 # 270582939648 bytes roughly equals 252GB
$disk_c = Get-PSDrive C | Select-Object Used,Free
# optionally use the boot drive instead of hard-coding the C drive
$disk_boot = Get-PSDrive (Get-WmiObject Win32_OperatingSystem).SystemDrive.SubString(0,1) | Select-Object Used,Free
echo 'capacity:'($disk_c.Used + $disk_c.Free)
echo 'threshold:'$threshold
If (($disk_c.Used + $disk_c.Free) -gt $threshold) {
echo 'should use HDD picture'
Set-WallPaper -value "C:\hdd.bmp"
} Else {
echo 'should use SSD picture'
Set-WallPaper -value "C:\ssd.bmp"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment