Skip to content

Instantly share code, notes, and snippets.

@GhostofGoes
Created September 22, 2018 03:21
Show Gist options
  • Save GhostofGoes/87ad972e36256f06fd8e5b8acaa4bfdb to your computer and use it in GitHub Desktop.
Save GhostofGoes/87ad972e36256f06fd8e5b8acaa4bfdb to your computer and use it in GitHub Desktop.
import sys
PY2 = sys.version_info[0] == 2
if PY2:
import _winreg as winreg
else:
import winreg
try:
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
ps_key = winreg.OpenKey(reg, r'SOFTWARE\Microsoft\PowerShell')
except OSError:
return None
ps_location = None
for version in [3, 1]:
ver_path = str(version) + r'\PowerShellEngine'
try:
ver_key = winreg.OpenKey(ps_key, ver_path)
ps_location = winreg.QueryValueEx(ver_key, 'ApplicationBase')[0]
except OSError:
continue
if not ps_location:
return None
ps_path = os.path.join(ps_location, 'powershell.exe')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment