Skip to content

Instantly share code, notes, and snippets.

@jhoneill
Last active November 20, 2020 22:05
Show Gist options
  • Save jhoneill/18ad1eb165fdeecea13c998eb3f9cdaa to your computer and use it in GitHub Desktop.
Save jhoneill/18ad1eb165fdeecea13c998eb3f9cdaa to your computer and use it in GitHub Desktop.
This script copies files downloaded by windows spotlight. For years I have had Windows background drawn from users\public\pictures\sample pictures, so this adds the spotlight ones to that location by default.
<#
.Synopsis
Copies images downloaded by Windows Spotlight
#>
[cmdletbinding()]
param (
#Where to send JPEGS which are Landscape format e.g ~\AppData\Roaming\Microsoft\Teams\Backgrounds\Uploads
$LandScapeDestination = "C:\users\Public\Pictures\Sample Pictures",
#Where to send JPEGS which are portrait format
$PortraitDestination = "C:\Users\Public\Pictures\Portrait Samples"
)
#Get hashes of exisiting images so if name changes we don't duplicate the file
Get-ChildItem -File -Path $LandScapeDestination,$PortraitDestination |
Get-FileHash -Algorithm md5 |
ForEach-Object -Begin {$hh=@{}} -Process {$hh[$_.hash] = $true }
#Image object used to check each file IS an image (if not we will skip it) and its dimensions
$image = New-Object -ComObject Wia.ImageFile
<#
Expect to find Windows Spotlight files in
"$Env:USERPROFILE\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
Files have NO extension but are JPEGS, but not everything in the folder is a JPEG or Background size
Allow for Content delivery manager to change its unique ID
#>
$Path = (Get-Item -Path "~\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager*").FullName
# Get files from there weeding out anything smaller than 50K or already copied
Get-ChildItem -Path "$path\LocalState\Assets" |
Where-Object {($_.length -GT 50Kb) -and
-not ((Test-Path "$LandScapeDestination\$($_.name).jpg") -or
(Test-Path "$PortraitDestination\$( $_.name).jpg") ) } |
Where-Object {-not $hh[(Get-FileHash $_)]} | ForEach-Object {
#Ensure the file is an image more than 1000 pixels wide; copy portrait and landscape files to their own destinations
try {
$image.LoadFile($_.FullName)
if ($image.Width -lt 1000) {Write-Verbose -Message "$($_.name) Skipped as too small, width=$($image.Width), Height=$($image.Height)"}
elseif ($image.Width -ge $image.Height) {
Copy-Item -Path $_.FullName -Destination ( Join-path -Path $LandScapeDestination -ChildPath ($_.name +".jpg")) -Verbose
}
else {Copy-Item -Path $_.FullName -Destination ( Join-path -Path $PortraitDestination -ChildPath ($_.name +".jpg")) -Verbose }
}
catch { Write-verbose -Message "Couldn't process $($_.Name)" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment