Skip to content

Instantly share code, notes, and snippets.

@eggbean
Last active October 14, 2024 18:50
Show Gist options
  • Save eggbean/315355ecdf17c41347e835ffafd08326 to your computer and use it in GitHub Desktop.
Save eggbean/315355ecdf17c41347e835ffafd08326 to your computer and use it in GitHub Desktop.
WinDirStat PowerShell script or registry file to add context menu entries to open on drives or folder, with a little icon
# Adds shell integration for WinDirStat. The program should be installed in
# Program Files (x86) but it will check if that directory is not on C:
#
# Alternatively, if installed on C: you can use the reg file below instead.
if (-not ([security.principal.windowsprincipal][security.principal.windowsidentity]::getcurrent()).isinrole([security.principal.windowsbuiltinrole]::administrator)) {
write-error "this script needs to be run as an administrator. please restart powershell as an administrator and try again."
exit
}
new-psdrive -name hkcr -psprovider registry -root hkey_classes_root
$windirstatpath = join-path ${env:programfiles(x86)} "windirstat\windirstat.exe"
if ([string]::isnullorempty($windirstatpath) -or !(test-path $windirstatpath)) {
write-error "windirstat not found at $windirstatpath. please check the installation path."
exit
}
function add-registryentry {
param (
[string]$path,
[hashtable]$properties
)
if (-not (test-path $path)) {
new-item -path $path -force | out-null
}
foreach ($prop in $properties.getenumerator()) {
set-itemproperty -path $path -Name $prop.Key -Value $prop.Value
}
}
$registryEntries = @(
@{
Path = "HKCR:\Directory\Background\shell\windirstat"
Properties = @{
"(Default)" = "Open WinDirStat here"
"Icon" = "$winDirStatPath,0"
}
},
@{
Path = "HKCR:\Directory\Background\shell\windirstat\command"
Properties = @{
"(Default)" = """$winDirStatPath"" ""%V"""
}
},
@{
Path = "HKCR:\Directory\shell\windirstat"
Properties = @{
"(Default)" = "Open WinDirStat here"
"Icon" = "$winDirStatPath,0"
}
},
@{
Path = "HKCR:\Directory\shell\windirstat\command"
Properties = @{
"(Default)" = """$winDirStatPath"" ""%1"""
}
},
@{
Path = "HKCR:\Drive\shell\windirstat"
Properties = @{
"(Default)" = "Open WinDirStat here"
"Icon" = "$winDirStatPath,0"
}
},
@{
Path = "HKCR:\Drive\shell\windirstat\command"
Properties = @{
"(Default)" = """$winDirStatPath"" ""%1\"""
}
}
)
foreach ($entry in $registryEntries) {
Add-RegistryEntry -Path $entry.Path -Properties $entry.Properties
}
Write-Host "Registry entries for WinDirStat have been added successfully."
Remove-PSDrive -Name HKCR
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\windirstat]
@="Open WinDirStat here"
"Icon"="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe,0"
[HKEY_CLASSES_ROOT\Directory\Background\shell\windirstat\command]
@="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\windirstat]
@="Open WinDirStat here"
"Icon"="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\windirstat\command]
@="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe \"%1\""
[HKEY_CLASSES_ROOT\Drive\shell\windirstat]
@="Open WinDirStat here"
"Icon"="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe,0"
[HKEY_CLASSES_ROOT\Drive\shell\windirstat\command]
@="C:\\Program Files (x86)\\WinDirStat\\windirstat.exe \"%1\\\""
@opus-x
Copy link

opus-x commented Feb 15, 2024

Thanks for this. Helped me to implement the context menu. The entry for the drive is wrong though, since it would start scanning only your profile folder. You'll need to add some extra backslashes. I also removed the hex-values and used the path names instead.

See my version for a portable setup:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\windirstat]
@="Analyze with WinDirStat"
"Icon"="P:\\bin\\system-tools\\windirstat\\windirstat.exe,0"

[HKEY_CLASSES_ROOT\Directory\shell\windirstat\command]
@="P:\\bin\\system-tools\\windirstat\\windirstat.exe \"%1\""

[HKEY_CLASSES_ROOT\Drive\shell\windirstat]
@="Analyze with WinDirStat"
"Icon"="P:\\bin\\system-tools\\windirstat\\windirstat.exe,0"

[HKEY_CLASSES_ROOT\Drive\shell\windirstat\command]
@="P:\\bin\\system-tools\\windirstat\\windirstat.exe \"%1\\\""

@eggbean
Copy link
Author

eggbean commented Aug 15, 2024

@opus-x Yes, it wasn't working on C: but worked fine on other drives. I've updating the reg file and now it works fine in all cases, including when clicking directories in the left or right explorer panes. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment