-
-
Save renini/712401710852029d4635b7711fdebae1 to your computer and use it in GitHub Desktop.
update wallpaper background image with powershell (like Sysinternals BGInfo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PS-BGInfo | |
# Powershell script that updates the background image with a random image from a folder and writes out system info text to it. | |
# Configuration: | |
# Font Family name | |
$font="Consolas" | |
# Font size in pixels | |
$size=10.0 | |
# spacing in pixels | |
$textPaddingLeft = 10 | |
$textPaddingTop = 10 | |
$textItemSpace = 3 | |
#TODO: Line-height multiplyer of the $size in pixels. | |
#$lineHeight = 1.80 | |
$wallpaperImagesSource = "$Env:USERPROFILE\Pictures\wallpaper" | |
$wallpaperImageOutput = "$Env:USERPROFILE" | |
# Get local info to write out to wallpaper | |
$os = Get-CimInstance Win32_OperatingSystem | |
$cpu = (Get-WmiObject Win32_Processor).Name.Replace("Intel(R) Core(TM) ", "") -replace '\s+', ' ' | |
$BootTimeSpan = (New-TimeSpan -Start $os.LastBootUpTime -End (Get-Date)) | |
$ip = (Get-NetIPAddress | Where-Object {$_.PrefixOrigin -eq "DHCP" -and $_.AddressFamily -eq "IPv4"}).IPAddress -join "`n" | |
$o = ([ordered]@{ | |
User = $os.RegisteredUser | |
Host = "$($os.CSName) `n$($os.Description)" | |
CPU = $cpu | |
RAM = "$([math]::round($os.TotalVisibleMemorySize / 1MB))GB" | |
OS = "$($os.Caption) `n$($os.OSArchitecture), $($os.Version)" | |
Boot = $os.LastBootUpTime | |
Uptime = "$($BootTimeSpan.Days) days, $($BootTimeSpan.Hours) hours" | |
Snapshot = $os.LocalDateTime | |
IP = $ip | |
}) | |
# original src: https://p0w3rsh3ll.wordpress.com/2014/08/29/poc-tatoo-the-background-of-your-virtual-machines/ | |
Function New-ImageInfo { | |
# src: https://github.com/fabriceleal/Imagify/blob/master/imagify.ps1 | |
param( | |
[Parameter(Mandatory=$True, Position=1)] | |
[object] $data, | |
[Parameter(Mandatory=$True)] | |
[string] $in="", | |
[string] $font="Courier New", | |
[float] $size=12.0, | |
#[float] $lineHeight = 1.4, | |
[float] $textPaddingLeft = 0, | |
[float] $textPaddingTop = 0, | |
[float] $textItemSpace = 0, | |
[string] $out="out.png" | |
) | |
[system.reflection.assembly]::loadWithPartialName('system') | out-null | |
[system.reflection.assembly]::loadWithPartialName('system.drawing') | out-null | |
[system.reflection.assembly]::loadWithPartialName('system.drawing.imaging') | out-null | |
[system.reflection.assembly]::loadWithPartialName('system.windows.forms') | out-null | |
$foreBrush = [System.Drawing.Brushes]::White | |
$backBrush = new-object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(192, 0, 0, 0)) | |
# Create font | |
$nFont = new-object system.drawing.font($font, $size, [System.Drawing.GraphicsUnit]::Pixel) | |
# Create Bitmap | |
$SR = [System.Windows.Forms.Screen]::AllScreens | Where Primary | Select -ExpandProperty Bounds | Select Width,Height | |
echo $SR >> "$wallpaperImageOutput\wallpaper.log" | |
$background = new-object system.drawing.bitmap($SR.Width, $SR.Height) | |
$bmp = new-object system.drawing.bitmap -ArgumentList $in | |
# Create Graphics | |
$image = [System.Drawing.Graphics]::FromImage($background) | |
# Paint image's background | |
$rect = new-object system.drawing.rectanglef(0, 0, $SR.width, $SR.height) | |
$image.FillRectangle($backBrush, $rect) | |
# add in image | |
$topLeft = new-object System.Drawing.RectangleF(0, 0, $SR.Width, $SR.Height) | |
$image.DrawImage($bmp, $topLeft) | |
# Draw string | |
$strFrmt = new-object system.drawing.stringformat | |
$strFrmt.Alignment = [system.drawing.StringAlignment]::Near | |
$strFrmt.LineAlignment = [system.drawing.StringAlignment]::Near | |
$taskbar = [System.Windows.Forms.Screen]::AllScreens | |
$taskbarOffset = $taskbar.Bounds.Height - $taskbar.WorkingArea.Height | |
# first get max key & val widths | |
$maxKeyWidth = 0 | |
$maxValWidth = 0 | |
$textBgHeight = 0 + $taskbarOffset | |
$textBgWidth = 0 | |
# a reversed ordered collection is used since it starts from the bottom | |
$reversed = [ordered]@{} | |
foreach ($h in $data.GetEnumerator()) { | |
$valString = "$($h.Value)" | |
$valFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Regular) | |
$valSize = [system.windows.forms.textrenderer]::MeasureText($valString, $valFont) | |
$maxValWidth = [math]::Max($maxValWidth, $valSize.Width) | |
$keyString = "$($h.Name): " | |
$keyFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Bold) | |
$keySize = [system.windows.forms.textrenderer]::MeasureText($keyString, $keyFont) | |
$maxKeyWidth = [math]::Max($maxKeyWidth, $keySize.Width) | |
$maxItemHeight = [math]::Max($valSize.Height, $keySize.Height) | |
$textBgHeight += ($maxItemHeight + $textItemSpace) | |
$reversed.Insert(0, $h.Name, $h.Value) | |
} | |
$textBgWidth = $maxKeyWidth + $maxValWidth + $textPaddingLeft | |
$textBgHeight += $textPaddingTop | |
$textBgX = $SR.Width - $textBgWidth | |
$textBgY = $SR.Height - $textBgHeight | |
$textBgRect = New-Object System.Drawing.RectangleF($textBgX, $textBgY, $textBgWidth, $textBgHeight) | |
$image.FillRectangle($backBrush, $textBgRect) | |
echo $textBgRect >> "$wallpaperImageOutput\wallpaper.log" | |
$i = 0 | |
$cumulativeHeight = $SR.Height - $taskbarOffset | |
foreach ($h in $reversed.GetEnumerator()) { | |
$valString = "$($h.Value)" | |
$valFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Regular) | |
$valSize = [system.windows.forms.textrenderer]::MeasureText($valString, $valFont) | |
$keyString = "$($h.Name): " | |
$keyFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Bold) | |
$keySize = [system.windows.forms.textrenderer]::MeasureText($keyString, $keyFont) | |
echo $valString >> "$wallpaperImageOutput\wallpaper.log" | |
echo $keyString >> "$wallpaperImageOutput\wallpaper.log" | |
$maxItemHeight = [math]::Max($valSize.Height, $keySize.Height) + $textItemSpace | |
$valX = $SR.Width - $maxValWidth | |
$valY = $cumulativeHeight - $maxItemHeight | |
$keyX = $valX - $maxKeyWidth | |
$keyY = $valY | |
$valRect = New-Object System.Drawing.RectangleF($valX, $valY, $maxValWidth, $valSize.Height) | |
$keyRect = New-Object System.Drawing.RectangleF($keyX, $keyY, $maxKeyWidth, $keySize.Height) | |
$cumulativeHeight = $valRect.Top | |
$image.DrawString($keyString, $keyFont, $foreBrush, $keyRect, $strFrmt) | |
$image.DrawString($valString, $valFont, $foreBrush, $valRect, $strFrmt) | |
$i++ | |
} | |
# Close Graphics | |
$image.Dispose(); | |
# Save and close Bitmap | |
$background.Save($out, [system.drawing.imaging.imageformat]::Png); | |
$background.Dispose(); | |
$bmp.Dispose(); | |
# Output file | |
Get-Item -Path $out | |
} | |
Function Set-Wallpaper { | |
# src: http://powershell.com/cs/blogs/tips/archive/2014/01/10/change-desktop-wallpaper.aspx | |
param( | |
[Parameter(Mandatory=$true)] | |
$Path, | |
[ValidateSet('Center', 'Stretch', 'Fill', 'Tile', 'Fit')] | |
$Style = 'Center' | |
) | |
#TODO: there in't a better way to do this than inline C#? | |
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32; | |
namespace Wallpaper | |
{ | |
public enum Style : int | |
{ | |
Center, Stretch, Fill, Fit, Tile | |
} | |
public class Setter { | |
public const int SetDesktopWallpaper = 20; | |
public const int UpdateIniFile = 0x01; | |
public const int SendWinIniChange = 0x02; | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); | |
public static void SetWallpaper ( string path, Wallpaper.Style style ) | |
{ | |
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); | |
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); | |
switch( style ) | |
{ | |
case Style.Tile : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "1") ; | |
break; | |
case Style.Center : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
case Style.Stretch : | |
key.SetValue(@"WallpaperStyle", "2") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
case Style.Fill : | |
key.SetValue(@"WallpaperStyle", "10") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
case Style.Fit : | |
key.SetValue(@"WallpaperStyle", "6") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
} | |
key.Close(); | |
} | |
} | |
} | |
"@ | |
[Wallpaper.Setter]::SetWallpaper( $Path, $Style ) | |
} | |
# execute tasks | |
echo $o > "$wallpaperImageOutput\wallpaper.log" | |
# get random wallpaper from a folder full of images | |
Get-ChildItem -Path "$wallpaperImagesSource\*" -Include *.* -Exclude current.jpg | Get-Random | Foreach-Object { Copy-Item -Path $_ -Destination "$wallpaperImagesSource\current.jpg" } | |
# create wallpaper image and save it in user profile | |
$WallPaper = New-ImageInfo -data $o -in "$wallpaperImagesSource\current.jpg" -out "$wallpaperImageOutput\wallpaper.png" -font $font -size $size -textPaddingLeft $textPaddingLeft -textPaddingTop $textPaddingTop -textItemSpace $textItemSpace #-lineHeight $lineHeight | |
echo $WallPaper.FullName >> "$wallpaperImageOutput\wallpaper.log" | |
# update wallpaper for logged in user | |
Set-Wallpaper -Path $WallPaper.FullName |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' VBScript for silently running the powershell script as a scheduled task | |
Dim shell,command | |
command = "powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -File D:\utils\PS-BGInfo.ps1" | |
set shell = CreateObject("WScript.Shell") | |
shell.Run command,0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment