LoginSignup
1
1

More than 3 years have passed since last update.

windows10のロック画面の画像を壁紙にも自動で反映させる

Posted at

windows10を利用していて、ロック画面に自動で現れる画像が毎回かっこいいので、壁紙にもシャッフルしてどれかを自動で設定するようにしました。
microsoftが自動で配信しているこんなやつです。
09ce27f81fe10e69dab84b9dcbe3596017470790c99959bb293a9683a9559f12.jpg

コードはwindows10の標準で利用できるようにpowershellで実現しています。
あまり、powershellに慣れてないので、コードの美しさについてはご愛嬌でお願いします。
とりあえず、動くものができました。
自動でmicrosoftから配信されているファイルをローカルにコピーしているので、ローカルの好きなフォルダを指定してください。
コマンドの実行は、手間はかからないのでログイン時に手動でポチポチやっても良いし、自動で実行したい場合は、タスクスケジューラに組み込むか、スタートアップのフォルダにでも入れて実行用のbatを実行してください。
あと、反映されないことが結構ありますが、それはwindowsの仕様です。
再起動すると確実に反映されるので、結局次回利用時には反映されています。
ちなみに、壁紙の変更にあたりレジストリ値を変更していますので、実行の際は自己責任でお願いします。

$sh = New-Object -ComObject Shell.Application

#ローカルフォルダ格納先
$localPath = 'C:\Users\xxx\Desktop\Assets'

#前日更新されたファイルを取得する
$localappdata = $env:LOCALAPPDATA
$assetsFileList = Get-ChildItem($localappdata + '\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets') | Where-Object{$_.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date}

#コピーしながら拡張子を.jpgに変更する
foreach($file in $assetsFileList){
    #もし同名のファイルがあればスキップする
    if (!(Test-Path ($localPath + '\' + $file + '.jpg'))){

        Copy-Item ($localappdata + '\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets' + '\' + $file) $localPath

        #拡張子の変更
        Rename-Item ($localPath + '\' + $file) ([System.IO.Path]::ChangeExtension(($localPath + '\' + $file), ".jpg"))
    }
}

#フォルダ取得
$f = $sh.Namespace($localPath)

#ファイル名の一覧を取得
$fileList = Get-ChildItem($localPath) -Name

[string[]]$array = @()

#ファイルサイズ(横縦)
#(1920×1080)の場合がPCの壁紙
foreach($file in $fileList){
    if ($f.GetDetailsOf($f.ParseName($file), 31) -eq "1920 x 1080"){
        $array += $file;
    }
}

#index値をシャッフルする
$randomNumber = Get-Random -Minimum 0 -Maximum ($array.Count - 1)

$filePath = $localPath + '\' + $array[$randomNumber]

Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop'  -Name 'Wallpaper' -Value "$filePath"

#繰り返さないとデスクトップの変更が反映されないらしい
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
rundll32.exe user32.dll,UpdatePerUserSystemParameters 1, True
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1