Skip to content

Instantly share code, notes, and snippets.

@devhawk
Created February 28, 2019 18:30
Show Gist options
  • Select an option

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

Select an option

Save devhawk/c54da59a357c916720dfa910a8b1f438 to your computer and use it in GitHub Desktop.
function Get-PythonVersions {
(get-item HKCU:\Software\Python\PythonCore).GetSubKeyNames()
}
function Get-DefaultPython {
$pythons = Get-PythonVersions | ForEach-Object{Get-ItemProperty "hkcu:\Software\Python\PythonCore\$_"}
$x64Pythons = $pythons | Where-Object{$_.SysArchitecture.StartsWith("64")} | Sort-Object -Property SysVersion -Descending
if ($x64Pythons.Length -gt 0)
{
return $x64Pythons[0].PSChildName
}
$x86Pythons = $pythons | Where-Object{$_.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
{
New-DynamicParameter -Name version -ValidateSet (Get-PythonVersions) -Type ([string])
}
process
{
$version = $PSBoundParameters["version"]
if ($null -eq $version)
{
$version = Get-DefaultPython
}
(Get-ItemProperty "hkcu:\Software\Python\PythonCore\$version\InstallPath")."(default)";
}
}
export-ModuleMember -Function Get-PythonVersions, Get-DefaultPython, Get-PythonFolder
Get-PythonVersions | ForEach-Object{
$folder = Get-PythonFolder -version $_
$suffix = $_.replace(".", "").replace("-32", "x86")
set-alias "py$suffix" (join-path $folder python.exe)
set-alias "dpy$suffix" (join-path $folder python_d.exe)
Export-ModuleMember -alias "py$suffix", "dpy$suffix"
}
$defaultPython = Get-DefaultPython
if ($null -ne $defaultPython) {
$folder = Get-PythonFolder -version $defaultPython
set-alias "dpy" (join-path $folder python_d.exe)
Export-ModuleMember -alias "dpy"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment