add type existence check for explorer refresh

This commit is contained in:
Chris Titus
2026-01-07 14:17:08 -06:00
parent da07c2040b
commit 1082a93813

View File

@@ -1,42 +1,37 @@
function Invoke-WinUtilExplorerUpdate { function Invoke-WinUtilExplorerUpdate {
<# <#
.SYNOPSIS .SYNOPSIS
Refreshes the Windows Explorer Refreshes the Windows Explorer
#> #>
param ( param (
[string]$action = "refresh" [string]$action = "refresh"
) )
if ($action -eq "refresh") { if ($action -eq "refresh") {
Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock { Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock {
# Send the WM_SETTINGCHANGE message to all windows # Define the Win32 type only if it doesn't exist
Add-Type -TypeDefinition @" if (-not ([System.Management.Automation.PSTypeName]'Win32').Type) {
Add-Type -TypeDefinition @"
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
public class Win32 { public class Win32 {
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessageTimeout( public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, IntPtr hWnd, uint Msg, IntPtr wParam, string lParam,
uint Msg, uint fuFlags, uint uTimeout, out IntPtr lpdwResult);
IntPtr wParam,
string lParam,
uint fuFlags,
uint uTimeout,
out IntPtr lpdwResult);
} }
"@ "@
}
$HWND_BROADCAST = [IntPtr]0xffff $HWND_BROADCAST = [IntPtr]0xffff
$WM_SETTINGCHANGE = 0x1A $WM_SETTINGCHANGE = 0x1A
$SMTO_ABORTIFHUNG = 0x2 $SMTO_ABORTIFHUNG = 0x2
$timeout = 100
# Send the broadcast message to all windows [Win32]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE,
[Win32]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [IntPtr]::Zero, "ImmersiveColorSet", $SMTO_ABORTIFHUNG, $timeout, [ref]([IntPtr]::Zero)) [IntPtr]::Zero, "ImmersiveColorSet", $SMTO_ABORTIFHUNG, 100,
[ref]([IntPtr]::Zero))
} }
} elseif ($action -eq "restart") { } elseif ($action -eq "restart") {
# Restart the Windows Explorer
taskkill.exe /F /IM "explorer.exe" taskkill.exe /F /IM "explorer.exe"
Start-Process "explorer.exe" Start-Process "explorer.exe"
} }