**Creating Custom Registry Hive for URL Protocol** As a PowerShell enthusiast, I'd like to share with you a script that creates a custom registry hive for a URL protocol. This script is useful when you need to register a new protocol handler in Windows. **Prerequisites**
The Script
The script starts by checking if the HKEY_CLASSES_ROOT registry hive exists. If it doesn't, it creates a new PowerShell drive to access the registry.
if (!(test-path "HKCR:\")) {
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR
}
Next, we set some variables for our script: the path to the registry hive, the key name for our protocol, and the default value for that key.
$classpath = "Registry::HKCR:\"
#$keyname = "MSEndpointMgrToastReboot"
$keyname = "MSUptimeToastReboot"
$keyDefault = "url:MSUptimeToastReboot"
$keyeditflags = "2162688"
$valcontent = 'C:\Windows\System32\shutdown.exe -r -t 60 -c "Your computer will be restarted in 1 minute, save any work you have open."'
We then set the location to the registry hive using `Set-Location`.
set-location -path HKCR:\
The script then checks if the key already exists. If it doesn't, it creates a new class protocol.
$classcheck = Get-ChildItem -Path "HKCR:\$($keyname)\" -Recurse -ErrorAction SilentlyContinue
if (!($classcheck)) {
#create class protocol
new-item -Path "HKCR:\" -Name $keyname -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "(Default)" -PropertyType "string" -Value $keydefault -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "EditFlags" -PropertyType "DWORD" -Value $keyeditflags -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "URL Protocol" -PropertyType "string" -Value "" -Verbose
new-item -Path "HKCR:\\$keyname" -Name "Shell" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell" -Name "Open" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell\Open" -Name "command" -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname\Shell\Open\command" -name "(Default)" -PropertyType "string" -Value $valcontent -Verbose
}
Finally, the script writes a success message to the console and exits.
write-output "Registry Hives Created, Compliant"
exit 0
How It Works
Creating the Registry Hive
The script starts by checking if the HKEY_CLASSES_ROOT registry hive exists. If it doesn't, it creates a new PowerShell drive to access the registry.
Setting Variables and Location
We set some variables for our script: the path to the registry hive, the key name for our protocol, and the default value for that key. We then set the location to the registry hive using `Set-Location`.
Checking for Key Existence and Creating Class Protocol
The script checks if the key already exists. If it doesn't, it creates a new class protocol. This involves creating several sub-keys under the key we're interested in.
Key Code Snippets
if (!(test-path "HKCR:\")) {
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR
}
$classpath = "Registry::HKCR:\"
#$keyname = "MSEndpointMgrToastReboot"
$keyname = "MSUptimeToastReboot"
$keyDefault = "url:MSUptimeToastReboot"
$keyeditflags = "2162688"
$valcontent = 'C:\Windows\System32\shutdown.exe -r -t 60 -c "Your computer will be restarted in 1 minute, save any work you have open."'
set-location -path HKCR:\
$classcheck = Get-ChildItem -Path "HKCR:\$($keyname)\" -Recurse -ErrorAction SilentlyContinue
if (!($classcheck)) {
#create class protocol
new-item -Path "HKCR:\" -Name $keyname -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "(Default)" -PropertyType "string" -Value $keydefault -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "EditFlags" -PropertyType "DWORD" -Value $keyeditflags -Verbose
new-itemproperty -Path "HKCR:\\$keyname" -name "URL Protocol" -PropertyType "string" -Value "" -Verbose
new-item -Path "HKCR:\\$keyname" -Name "Shell" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell" -Name "Open" -ItemType directory -Verbose
new-item -Path "HKCR:\\$keyname\Shell\Open" -Name "command" -ItemType directory -Verbose
new-itemproperty -Path "HKCR:\\$keyname\Shell\Open\command" -name "(Default)" -PropertyType "string" -Value $valcontent -Verbose
}
Usage Examples
This script is useful when you need to register a new protocol handler in Windows. Simply run the script and it will create the necessary registry hive and keys for your custom URL protocol.
Conclusion
In this blog post, we've explored a PowerShell script that creates a custom registry hive for a URL protocol. The script sets variables, checks for key existence, and creates several sub-keys under the key we're interested in. With this script, you can register your own custom protocol handler in Windows.