Skip to content

Instantly share code, notes, and snippets.

@mkropat
Created March 27, 2019 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkropat/c9953e0ee9a81da048b5e526263923bc to your computer and use it in GitHub Desktop.
Save mkropat/c9953e0ee9a81da048b5e526263923bc to your computer and use it in GitHub Desktop.
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[string] $Name
)
$ErrorActionPreference = 'Stop'
if (-not [System.IO.Path]::GetExtension($Name)) {
$Name = $Name + '.exe'
}
$programDirs = @($env:ProgramFiles, ${env:ProgramFiles(x86)}, $env:ProgramData)
$path = Get-ChildItem $programDirs -Recurse -Include $Name -ErrorAction SilentlyContinue |
Select-Object -First 1 -ExpandProperty FullName
if (-not $path) {
Write-Error "Unable to locate $Name in $programDirs"
exit 1
}
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\$Name"
if (Test-Path $regPath) {
Set-ItemProperty $regPath -Name '(default)' -Value $path
}
else {
New-Item $regPath | Out-Null
New-ItemProperty $regPath -Name '(default)' -Value $path | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment