Last active
April 14, 2019 05:41
-
-
Save Trucido/1f6b9e5326b80f0573b1fbf22bf0927b to your computer and use it in GitHub Desktop.
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
<# | |
.SYNOPSIS | |
Copies Windows Spotlight images from ContentDeliverManager cache folder to "Pictures" folder. | |
.DESCRIPTION | |
Copies all images larger than 50KB from Windows Spotlight cache folder to \Pictures\Backgrounds\Spotlight, | |
creating destination directory if it does not exist. | |
.NOTES | |
Requires Lock Screen Background set to "Windows Spotlight" (Start-Process ms-settings:lockscreen). | |
Spotlight cache folder will populate over time, with new images usually added several times per week. | |
Caveat: Windows Store also stores images in the same dir, some may be mistaken as a spotlight images. | |
Tip: To sort by resolution; | |
Open ${env:USERPROFILE}\Pictures\Backgrounds\Spotlight; View>Details; Right click a column>"dimensions" | |
#> | |
function Copy-WindowsSpotlightLockscreenImages | |
{ | |
# Destination to save images to. | |
$imageDestinationPath = "$env:USERPROFILE\Pictures\Backgrounds\Spotlight" | |
# Verbose output if true | |
$verbose = $true | |
# Create destination folder if not exist | |
if (!(Test-Path $imageDestinationPath -Type Container)) | |
{ | |
try | |
{ | |
New-Item $imageDestinationPath -ItemType "Directory" -Verbose:$verbose | |
#mkdir $imageDestinationPath # Note: Microsoft.PowerShell.Core\Function::mkdir | |
} | |
catch | |
{ | |
Write-Debug $_.Exception.Message | |
throw $_ | |
} | |
} | |
# Spotlight cache path | |
$WindowsSpotlightPath = "$env:USERPROFILE\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" | |
if (!(Test-Path $WindowsSpotlightPath)) { throw 'Error: Something Happened.' } | |
# All items in folder that aren't a directory and are larger than 50KB | |
$images = Get-ChildItem ${WindowsSpotlightPath}\* -Force | Where-Object{ !$_.PSIsContainer -and $_.Length / 1KB -gt 50 } | Foreach-Object{$_} | |
# ForEach image try and copy it to destination | |
# This should probably be a process{} block... | |
foreach($item in $images) | |
{ | |
try | |
{ | |
# Try/Catch needed due to ErrorAction not ignoring all Get-Item errors | |
$item = try { Get-Item $item -Force -ErrorAction:Ignore | Select-Object -Property Extension,FullName,BaseName } catch {} | |
if (!$item -or !$item.BaseName) | |
{ | |
Write-Debug "Error processing file: $($item | Format-List | Out-String)" | |
continue | |
} | |
# Skip existing images in destination path | |
if (Test-Path ${imageDestinationPath}\${item.BaseName}*) | |
{ | |
continue | |
} | |
# Add ".jpg" to items without extension | |
if (!$item.Extension -or !($item.Extension -in ".jpg",".jpeg",".png")) | |
{ | |
$imageName = $item.BaseName + ".jpg" | |
} | |
else | |
{ | |
$imageName = $item.BaseName | |
} | |
# Combine full destination path with imageName | |
$imageToProcess = Join-Path -Path $imageDestinationPath -ChildPath $imageName -ErrorAction:Ignore | |
if (!$imageToProcess) { continue } | |
# Perform copy operation on item if $item != String.IsNullOrEmpty | |
if (!($imageToProcess -eq $null -or $imageToProcess -eq '')) | |
{ | |
Copy-Item -LiteralPath $item.FullName -Destination $imageToProcess -Verbose:$verbose | |
} | |
else | |
{ | |
continue | |
} | |
} | |
catch | |
{ | |
Write-Debug $_.Exception.Message # Write-Host $_.Exception.Message -ForegroundColor 'Red' | |
continue | |
} | |
} | |
} | |
# Invoke the embedded function if this script is run directly as apposed to being dot-sourced. | |
if (-not($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq '')) | |
{ | |
Copy-WindowsSpotlightLockscreenImages @args | |
} | |
# Optionally open images folder. (Invoke default handler for paths, explorer) | |
#Invoke-Item "$env:USERPROFILE\Pictures\Backgrounds\Spotlight\" | |
# Local Variables: | |
# mode: PowerShell; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; | |
# End: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment