Skip to content

Instantly share code, notes, and snippets.

@devhawk
Last active February 26, 2019 21:10
Show Gist options
  • Select an option

  • Save devhawk/8354a8304b4c47d4ff758de639bff1ed to your computer and use it in GitHub Desktop.

Select an option

Save devhawk/8354a8304b4c47d4ff758de639bff1ed to your computer and use it in GitHub Desktop.
Powershell utilities for locating Python installations from Windows registry
function Get-DefaultPython {
$pythonVersions = (get-item HKCU:\Software\Python\PythonCore).GetSubKeyNames()
$pythons = $pythonVersions | %{Get-ItemProperty "hkcu:\Software\Python\PythonCore\$_"}
# favor most recent version of Python and x64 version of Python over x86 version
$x64Pythons = $pythons | ?{$_.SysArchitecture.StartsWith("64")} | Sort-Object -Property SysVersion -Descending
if ($x64Pythons.Length -gt 0)
{
return $x64Pythons[0].PSChildName
}
$x86Pythons = $pythons | ?{$_.SysArchitecture.StartsWith("32")} | Sort-Object -Property SysVersion -Descending
if ($x86Pythons.Length -gt 0)
{
return $x86Pythons[0].PSChildName
}
throw "Python not installed"
}
function Get-PythonFolder {
[CmdletBinding()]
Param()
DynamicParam
{
$pythonVersions = (get-item HKCU:\Software\Python\PythonCore).GetSubKeyNames()
# from https://github.com/beatcracker/Powershell-Misc/blob/master/New-DynamicParameter.ps1
New-DynamicParameter -Name version -ValidateSet $pythonVersions -Type ([string])
}
process
{
$version = $PSBoundParameters["version"]
if ($version -eq $null)
{
$version = Get-DefaultPython
}
(Get-ItemProperty "hkcu:\Software\Python\PythonCore\$version\InstallPath")."(default)";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment