Skip to content

Instantly share code, notes, and snippets.

@JulianPritchard
Last active September 20, 2022 09:41
Show Gist options
  • Save JulianPritchard/4f040c16e15aa1e694d6125f944f9cab to your computer and use it in GitHub Desktop.
Save JulianPritchard/4f040c16e15aa1e694d6125f944f9cab to your computer and use it in GitHub Desktop.
Powershell7 script to add context menu shortcuts for Jetbrains IDEs
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string] $IdeName,
[Parameter(Mandatory=$true)]
[string] $IdeBinFolder,
[string] $Ext="sln",
[switch] $CreateDiffShortcut=$false
)
$main = {
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR | Out-Null
Add-Shortcut-For $IdeName $IdeBinFolder
$EnvironmentVariable = Add-Environment-Variable $IdeName $IdeBinFolder
Update-Open-With $Ext
if ($CreateDiffShortcut){
Add-DiffShortcut $IdeName $EnvironmentVariable
}
}
function Add-Shortcut-For {
param (
[string] $IdeName,
[string] $IdeBinFolder
)
$RegRoot = "HKCR:"
Add-Registry-For-Path (Join-Path $RegRoot "*\shell\${IdeName}") $IdeBinFolder $IdeName
Add-Registry-For-Path (Join-Path $RegRoot "Directory\shell\${IdeName}") $IdeBinFolder $IdeName
Add-Registry-For-Path (Join-Path $RegRoot "Directory\background\shell\${IdeName}") $IdeBinFolder $IdeName
Add-Registry "${RegRoot}\Applications$(Get-ExeName($IdeName))\shell\open\command" "(Default)" "$(Get-ExePath) %1" "String" $false
}
function Add-Registry-For-Path {
param (
[string] $FullRegPath,
[string] $IdeBinFolder,
[string] $IdeName
)
$Default = "(Default)"
Add-Registry $FullRegPath $Default "Open with $IdeName" "String"
Add-Registry $FullRegPath "Icon" (Join-Path "$IdeBinFolder" "${IdeName}.ico") "ExpandString"
Add-Registry (Join-Path "$FullRegPath" command) $Default "$(Get-ExePath) %1" "String"
}
function Add-Registry {
param (
[string] $RegistryPath,
[string] $Name,
[System.Object] $Value,
[string] $PropertyType,
[bool] $createIfMissing = $true
)
# Create the key if it does not exist
If (-NOT (Test-Path -LiteralPath $RegistryPath))
{
if ($createIfMissing)
{
New-Item -Path $RegistryPath -Force | Out-Null
}
else
{
return
}
}
# Now set the value
New-ItemProperty -LiteralPath $RegistryPath -Name $Name -Value $Value -PropertyType $PropertyType -Force
}
function Update-Registry {
param (
[string] $RegistryPath,
[string] $Name,
[System.Object] $Value,
[string] $PropertyType
)
If (-NOT (Test-Path -LiteralPath $RegistryPath))
{
return
}
New-ItemProperty -LiteralPath $RegistryPath -Name $Name -Value $Value -PropertyType $PropertyType -Force
}
function Add-Environment-Variable {
param (
[string] $IdeName,
[string] $IdeBinFolder
)
$PathName = "${IdeName}_PATH".ToUpper()
[System.Environment]::SetEnvironmentVariable($PathName, $IdeBinFolder, [System.EnvironmentVariableTarget]::Machine)
Write-Host "Adding Path '$PathName' as '$IdeBinFolder'" -ForegroundColor Cyan
return $PathName
}
function Update-Open-With {
param (
[string] $ext
)
$ext = $ext.Trim()
$extName = "${ext}file"
$exePath = Get-ExePath
Write-Host "Associating $ext files with $exePath" -ForegroundColor Cyan
cmd /c assoc ".$ext=$extName"
cmd /c "ftype $extName=`"$exePath`" `"%1`""
}
function Add-DiffShortcut
{
param(
[string] $IdeName,
[string] $EnvironmentVariable
)
$WshShell = New-Object -comObject WScript.Shell
$ShortcutName = Join-Path $WshShell.SpecialFolders("SendTo") "_${IdeName}Diff.lnk"
$Arguments = "diff"
$WorkingDir = "$env:USERPROFILE"
$TargetPath = "%${EnvironmentVariable}%\$(Get-ExeName $IdeName)"
$Shortcut = $WshShell.CreateShortcut($ShortcutName)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Arguments = $Arguments
$Shortcut.WorkingDirectory = $WorkingDir
$Shortcut.Save()
Write-Host "Adding Shortcut`nLink: $ShortcutName`nTarget: $TargetPath`nArguments: $Arguments`nWorking Directory: $WorkingDir" -ForegroundColor Blue
}
function Get-ExePath {
return Join-Path $IdeBinFolder (Get-ExeName $IdeName)
}
function Get-ExeName {
param (
[string] $Name
)
return "$($Name.ToLower())64.exe"
}
& $main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment