Advertisement
Guest User

Copy-LockscreenImages.ps1

a guest
Jan 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # PowerShell script to copy the images Windows 10 uses for its lockscreen to
  2. # the users Pictures folder in two subdirectories:
  3. # *'Wallpapers widescreen' (for images that are wider than they are tall) and
  4. # *'Wallpapers longscreen' (for images that are taller than they are wide).
  5. # The subdirectories will be created if they do not exist.
  6. #
  7. # Run this script (as admin) with '-task' to create a scheduled task that will
  8. # automatically run this script when you log in. The script will register
  9. # itself at its current location, with its current filename. Put the script in
  10. # a place where you want to keep it before creating the task!
  11.  
  12. param(
  13.     [switch]$task = $false
  14. )
  15.  
  16. # Create the scheduled task
  17. if($task){
  18.     $description = "Copies the images that Windows uses on the lockscreen to Pictures so that they can be used as wallpapers"
  19.     $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument ('-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "'+$PSCommandPath+'"')
  20.     $trigger =  New-ScheduledTaskTrigger -AtLogOn -User ([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
  21.     Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Copy Lockscreen Images" -Description $description
  22. }
  23.  
  24. add-type -AssemblyName System.Drawing
  25.  
  26. # This is the secret path where Windows 10 keeps its lockscreen images. If you
  27. # open this folder yourself, you'll see a bunch of files with long hexadecimal
  28. # numbers as names and no extensions. If you rename the files to
  29. # 'something.jpg' you'll find that some, but not all, are actually images. The
  30. # folder includes all sorts of images used by Windows, including the icons for
  31. # Candy Crush and other 'promotional' games. But it also includes our precious
  32. # lockscreen images in both landscape and portrait orientations.
  33. $source_path = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\"
  34.  
  35. # We'll store the images in you 'Pictures' folder.
  36. $base_path = [environment]::GetFolderPath('MyPictures')
  37.  
  38. # This is the path for landscape images.
  39. $wide_path = Join-Path -Path $base_path -ChildPath "Wallpapers widescreen\"
  40. if(!(Test-Path $wide_path)){
  41.     New-Item -ItemType Directory -Force -Path $wide_path
  42. }
  43. # This is the path for portrait images.
  44. $long_path = Join-Path -Path $base_path -ChildPath "Wallpapers longscreen\"
  45. if(!(Test-Path $long_path)){
  46.     New-Item -ItemType Directory -Force -Path $long_path
  47. }
  48.  
  49. # Let's get a list of all files in our source path.
  50. $filelist = Get-ChildItem $source_path
  51. foreach($file in $filelist){
  52.     # Try to load the file as an image.
  53.     try{
  54.         $img = [System.Drawing.Image]::FromFile($file.FullName);
  55.     }
  56.     # If this fails, it wasn't an image and we can skip to the next file.
  57.     catch{
  58.         continue;
  59.     }
  60.     # If the image is smaller than 1 Megapixel it's too small for a wallpaper.
  61.     if($img.width * $img.height -lt 1000000){
  62.         continue;
  63.     }
  64.    
  65.     # Add ".jpg" to the name of the file and copy it the the right directory.
  66.     # While a long hexadecimal number might not be the catchiest of names, at
  67.     # least it's unique and helps us avoid multiple copies of the same image.
  68.     $target_path = $file.name + ".jpg"
  69.     if($img.width -gt $img.height){
  70.         $target_path = Join-Path -Path $wide_path -ChildPath $target_path
  71.     } else {
  72.         $target_path = Join-Path -Path $long_path -ChildPath $target_path
  73.     }
  74.     Copy-Item $file.FullName $target_path
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement