Skip to content

Instantly share code, notes, and snippets.

@patrickgreenwell
Forked from spSlaine/SaveSpotlightImages.ps1
Created September 26, 2017 14:18
Show Gist options
  • Save patrickgreenwell/a7a985b94d6558f0b75903a5c5922209 to your computer and use it in GitHub Desktop.
Save patrickgreenwell/a7a985b94d6558f0b75903a5c5922209 to your computer and use it in GitHub Desktop.
Copies the Windows Spotlight lock screen images in Windows 10 to a "Windows Spotlight" folder in My Pictures.
<#
.DESCRIPTION
Copies the Windows Spotlight lock screen images in Windows 10 to a "Windows Spotlight" folder in My Pictures.
This script will intelligently sort through the temporary directory and will only copy images
that are 1920x1080. Since the filenames of the images can change, the script will also compare
SHA1 hashes of the existing so we don't copy duplicates.
.NOTES
Version: 1.0.4
Author: jcefoli
Creation Date: 3/14/2016
Only tested in Powershell 4 on Windows 10
.EXAMPLE
Run from the Command Prompt (As Admin) like so:
Straight from GitHub:
@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://gist.github.com/patrickgreenwell/a7a985b94d6558f0b75903a5c5922209/raw/SaveSpotlightImages.ps1'))"
Local Path (assumes you download the script to the C:\ root)"
@powershell -NoProfile -ExecutionPolicy Bypass C:\SaveSpotlightImages.ps1
You Can set this to run windowless by using a VB Script like the following (note you need to double escape the double-quotes in the powershell command:
Dim shell,command
command = "powershell -NoProfile -ExecutionPolicy Bypass -Command ""iex ((new-object net.webclient).DownloadString('https://gist.github.com/patrickgreenwell/a7a985b94d6558f0b75903a5c5922209/raw/SaveSpotlightImages.ps1'))"""
Set shell = CreateObject("WScript.Shell")
shell.Run command,0
#>
$StartTime = Get-Date
###------------------------------------------------------------------------------------------###
### SET IMAGE COPY LOCATION HERE ###
### Defaults to "Windows Spotlight" folder in your Pictures ###
$horizontalImageRestorePath = "$env:USERPROFILE\Pictures\Windows Spotlight"
$verticalImageRestorePath = "$env:USERPROFILE\Pictures\Windows Spotlight (Vertical)"
###------------------------------------------------------------------------------------------###
#Clear terminal and add loading message
Clear-Host
Write-Host ""
Write-Host " Working some magic. Please wait! "
Write-Host ""
#Load system.drawing Assembly
[void][reflection.assembly]::loadwithpartialname("system.drawing")
#Function to Get Image Metadata
function Get-Image{
process {
$file = $_
[Drawing.Image]::FromFile($_.FullName) |
ForEach-Object{
$_ | Add-Member -PassThru NoteProperty FullName ('{0}' -f $file.FullName)
$_.Dispose()
}
}
}
#Create Temporary Directory
$checkDir = Test-Path $env:TEMP\bgtempimages
If ($checkDir -eq $False){
New-Item $env:TEMP\bgtempimages -Type directory | Out-Null
}
Else {
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder("$env:TEMP\bgtempimages*")
}
#Copy Assets from Windows Location to Temp Directory
Copy-Item $env:LOCALAPPDATA\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\* -Destination $env:TEMP\bgtempimages\
#Add .jpg Extension to Files
Get-ChildItem -Path "$env:TEMP\bgtempimages" | Rename-Item -newname { $_.Name + ".jpg" }
#Detect and Remove Rogue XML Files
$tempFileList = Get-ChildItem -Path "$env:TEMP\bgtempimages" | Select-Object FullName
ForEach ($file in $tempFileList) {
$isXML = [bool]((Get-Content $file.FullName) -as [xml])
if ($isXML -eq $True) {
Remove-Item $file.FullName
}
}
#Create Permanent Background Images Directory if it Doesn't Exist
$checkDir = Test-Path $horizontalImageRestorePath
If ($checkDir -eq $False){
New-Item $horizontalImageRestorePath -Type directory | Out-Null
}
$checkDir = Test-Path $verticalImageRestorePath
If ($checkDir -eq $False){
New-Item $verticalImageRestorePath -Type directory | Out-Null
}
#Folder Exists, so grab the SHA1 hashes of all the existing images so we don't copy duplicate backgrounds with different filenames
$existingImageObjects = Get-ChildItem -Path $horizontalImageRestorePath | Select-Object -expa Fullname
$existingImageObjects += Get-ChildItem -Path $verticalImageRestorePath | Select-Object -expa FullName
#Add all hashes in the permanent image directory to $existingHashesArray
$existingHashesArray = @()
ForEach ($filepath in $existingImageObjects ) {
$existingHashesArray += Get-FileHash -Path $filepath -Algorithm SHA1
}
#Detect images that are 1920x1080 (The background images we want to move)
$horizontalImagesToCopy = Get-ChildItem -Path "$env:TEMP\bgtempimages" -Filter *.* -Recurse | Get-Image | Where-Object { $_.Width -eq 1920 -or $_.Height -eq 1080 } | Select-Object -expa Fullname
$verticalImagesToCopy = Get-ChildItem -Path "$env:TEMP\bgtempimages" -Filter *.* -Recurse | Get-Image | Where-Object { $_.Width -eq 1080 -or $_.Height -eq 1920 } | Select-Object -expa Fullname
#Get the Hashes of those 1920x1080 images and store in $newHorizontalImageHashArray
$newHorizontalImageHashArray = @()
ForEach ($filepath in $horizontalImagesToCopy ) {
$newHorizontalImageHashArray += Get-FileHash -Path $filepath -Algorithm SHA1
}
$newVerticalImageHashArray = @()
ForEach ($filepath in $verticalImagesToCopy ) {
$newVerticalImageHashArray += Get-FileHash -Path $filepath -Algorithm SHA1
}
#Loop through Temp Horizontal Images To Copy
$i = 0
$newHorizontalImageHashArray | ForEach-Object {
if ($existingHashesArray.Hash -Contains $_.Hash){
#Found duplicate hash in existing directory, do not copy new image over
}
Else {
#New background found! Copy it over
Copy-Item $_.Path -Destination $horizontalImageRestorePath
$i++
}
}
#Loop through Temp Vertical Images To Copy
$j = 0
$newVerticalImageHashArray | ForEach-Object {
if ($existingHashesArray.Hash -Contains $_.Hash){
#Found duplicate hash in existing directory, do not copy new image over
}
Else {
#New background found! Copy it over
Copy-Item $_.Path -Destination $verticalImageRestorePath
$j++
}
}
#Status output
$FinishTime = Get-Date
$TotalTime = ($FinishTime - $StartTime).TotalMilliseconds
if ($i -eq 0) {
Write-host "[Done] - No images copied. Took $TotalTime ms." -foregroundcolor "Yellow"
}
ElseIF ($i -eq 1){
Write-host "[Done] - Copied $i image. Took $TotalTime ms." -foregroundcolor "Green"
}
Else{
Write-host "[Done] - Copied $i images. Took $TotalTime ms." -foregroundcolor "Green"
}
#Delete Temp Folder
$fso = New-Object -ComObject scripting.filesystemobject
$fso.DeleteFolder("$env:TEMP\bgtempimages*")
@Kenya-West
Copy link

Thanks for such a good script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment