Created
April 26, 2016 23:57
-
-
Save bosconian-dynamics/e3ba64d066785407b47d0a47fb4fcbde to your computer and use it in GitHub Desktop.
A simple PowerShell script that copies images from the Windows 10 Spotlight cache to the user's Pictures directory. By default, skips images less than 180 kb in size. Works well as a Windows scheduled task
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
$cacheDir = "$($env:LOCALAPPDATA)\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" | |
$destDir = "$($env:HOME)\Pictures\Spotlight" | |
$lastSync = 0 | |
$sizeThreshold = 184320 | |
function Read-CacheFiles { | |
Get-ChildItem $cacheDir | |
} | |
function Read-CopiedFiles { | |
$filenames = @() | |
Get-ChildItem $destDir | ForEach-Object { | |
$filenames += $_.Name -replace '.jpg','' | |
if( $_.LastWriteTime -gt $script:lastSync ) { | |
$script:lastSync = $_.LastWriteTime | |
} | |
} | |
$filenames | |
} | |
function Copy-Images { | |
$copiedFilenames = Read-CopiedFiles | |
$copyCount = 0 | |
if( $script:lastSync -gt 0 ) { | |
Write-Host "Last Sync : $($script:lastSync)" | |
} | |
Write-Host "`r`n" | |
Read-CacheFiles | ForEach-Object { | |
#Write-Host ( $_ | Format-Table | Out-String ) | |
if( $_.LastWriteTime -lt $script:lastSync ) { | |
Write-Host "Skipping $($_.Name): Old Write Time ($($_.LastWriteTime))" | |
Return | |
} | |
if( $_.Length -lt $sizeThreshold ) { | |
Write-Host "Skipping $($_.Name): Too Small ($($_.Length) bytes)" | |
Return | |
} | |
if( $copiedFilenames -contains $_.Name ) { | |
Write-Host "Skipping $($_.Name): Already Copied" | |
Return | |
} | |
Write-Host "Copying $($_.Name)" | |
Copy-Item "$cacheDir\$($_.Name)" "$destDir\$($_.Name).jpg" | |
$copyCount++ | |
} | |
$copyCount | |
} | |
try { | |
if( ! ( Test-Path -PathType Container $destDir ) ) { | |
New-Item -ItemType Directory -Force -Path $destDir | Out-Null | |
} | |
if( $sizeThreshold -gt 0 ) { | |
Write-Host "Size Threshold : $($sizeThreshold / 1024) kb" | |
} | |
$new = Copy-Images | |
if( $new -gt 0 ) { | |
Write-Host "`r`n$new new Spotlight images preserved!" | |
} | |
else { | |
Write-Host "`r`nNo new Spotlight images" | |
} | |
} | |
finally { | |
Write-Host "Complete" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script may slow down to some degree if the destination directory is not cleared from time to time.