Created
March 11, 2022 06:13
-
-
Save Mufaddal1125/5f21b8fbd0ad751bfdb97a52854b25af to your computer and use it in GitHub Desktop.
PowerShell Script to automatically save screen snips on windows
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
# Add this script to Task Scheduler | |
# and schedule it to run at logon | |
# in the action start a powershell program and give the arguments | |
# -Command "Get-Content -Path '<Path To Script>' -Raw | Invoke-Expression" | |
$watcher = New-Object System.IO.FileSystemWatcher; | |
$watcher.Path = "$HOME\AppData\Local\Packages\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\TempState\ScreenClip"; | |
$watcher.Filter = "*.png"; | |
$watcher.NotifyFilter = "FileName"; | |
$watcher.EnableRaisingEvents = $true; | |
$action = { | |
$path = $Event.SourceEventArgs.FullPath; | |
# Last chance for a file to unlock; | |
Start-Sleep -Milliseconds 100; | |
try { | |
Add-Type -AssemblyName System.Drawing; | |
# Open image file; | |
$png = [System.Drawing.Image]::FromFile((Get-Item $path)); | |
$dimensions = -join ($png.Width, " x ", $png.Height); | |
# Skip automatic thumbnails which are always the same size; | |
If ($dimensions -ne "364 x 180") { | |
# Save the image to destination; | |
# Note: the destination directory should exist; | |
Copy-Item -Path $path -Destination "$HOME\Pictures\Screenshots\Screenshot-$(get-date -f yyyy-MM-dd-HHmmss.fff).png"; | |
} | |
} catch { | |
Write-Host "Problem acquiring image"; | |
} finally { | |
$png.Dispose(); | |
} | |
} | |
Register-ObjectEvent -InputObject $watcher -EventName "Created" -SourceIdentifier 'SnipGrab' -Action $action; | |
Remove-Item -Path (Join-Path $watcher.Path '\*') -Recurse; | |
while ($true){ Wait-Event -SourceIdentifier "SnipGrab"; }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment