Skip to content

Instantly share code, notes, and snippets.

@asheroto
Created December 24, 2021 03:53
Show Gist options
  • Save asheroto/e0a75920857487dd3dbaf1bac79fc5bc to your computer and use it in GitHub Desktop.
Save asheroto/e0a75920857487dd3dbaf1bac79fc5bc to your computer and use it in GitHub Desktop.
PowerShell script to configure a custom protocol in Windows.

Custom Protocol

PowerShell script that adds a custom protocol handler in Windows.

Example

mynewprotocol://this_would/be_the/arguments

Instructions

Change the $ProtocolName and $ProtocolExePath variables.

$ProtocolName - Name for your protocol, needs to be one word, without spaces or special characters, such as MyNewProtocol

$ProtocolExePath - Program that will handle the protocol and its arguments

# Custom Protocol
#
# Description:
# Creates a new custom protocol in Windows
#
# Note:
# Be sure to change the variables before running!
#
# Author: asheroto
# Author GitHub: https://github.com/asheroto
#
# Version 0.1
#Requires -RunAsAdministrator
$ProtocolName = "MyNewProtocol"
$ProtocolExePath = "C:\MyNewProtocolHandler.exe"
Write-Output ""
Write-Output "Starting..."
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ea SilentlyContinue | Out-Null
New-Item -Path "HKCR:" -Name "$ProtocolName" –Force | Out-Null
Set-ItemProperty -Path "HKCR:\$ProtocolName" -Name "(Default)" -Value "URL:$ProtocolName Protocol" | Out-Null
Set-ItemProperty -Path "HKCR:\$ProtocolName" -Name "URL Protocol" -Value '""' | Out-Null
New-Item -Path "HKCR:\$ProtocolName" -Name "shell" –Force | Out-Null
New-Item -Path "HKCR:\$ProtocolName\shell" -Name "open" –Force | Out-Null
New-Item -Path "HKCR:\$ProtocolName\shell\open" -Name "command" –Force | Out-Null
Set-ItemProperty -Path "HKCR:\$ProtocolName\shell\open\command" -Name "(Default)" -Value """$ProtocolExePath"" ""%1""" | Out-Null
Write-Output ""
Write-Output "Completed registry setup"
Write-Output ""
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment