【PowerShell】コマンドで壁紙を変更する
//
まずググると
いろんな情報が出てきますね。ただ、Windows10
に対して有効な手段が見つけにくかったので、ここに残しておきます。
有効であったもの
$setwallpapersource = @" using System.Runtime.InteropServices; public class wallpaper { public const int SetDesktopWallpaper = 20; public const int UpdateIniFile = 0x01; public const int SendWinIniChange = 0x02; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); public static void SetWallpaper ( string path ) { SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); } } "@ Add-Type -TypeDefinition $setwallpapersource [wallpaper]::SetWallpaper("画像があるパス")
上のコマンドを管理者の権限で実行してください。ps
ファイルで一度に実行したい場合はpowershell
でスクリプトが読めるようにしてから行ってください。
注意としては、最後の画像があるパスはユーザのフォルダ以下になるとなぜか反映されません。なので、例えばC
直下とか、C:¥Users¥
内であれば読み込まれました。
微妙だったもの
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d wallpaper_path /f RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
一応変更は確認できましたが、2行目の変更を反映させるコマンドが不安定で、何十回も押す必要がありました。確実性がなかったので、今回は使いませんでした。
余談
例えば、webサイトから画像を引っ張ってきてそれを壁紙にする場合下の流れになります。
$setwallpapersource = @" using System.Runtime.InteropServices; public class wallpaper { public const int SetDesktopWallpaper = 20; public const int UpdateIniFile = 0x01; public const int SendWinIniChange = 0x02; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); public static void SetWallpaper ( string path ) { SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); } } "@ Add-Type -TypeDefinition $setwallpapersource [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri 画像の直リンなど -OutFile C:¥Users¥.w.jpg Start-Sleep -s 10 #ダウンロードが終わるのを念の為まつ [wallpaper]::SetWallpaper("C:¥Users¥.w.jpg")