1# Windows PowerShell Script to audit the 'Machine' PATH
2# and the 'User' PATH environment variable for path conflicts,
3# which could jeopardize 'pyenv'-related Python calls.
8# © 2025 Michael Paul Korthals. All rights reserved.
9# For legal details see documentation.
13# This script is located in the 'pyenv-virtualenv' plugin root directory.
15# It will be called by 'install.bat' its parent folder.
17# The script returns RC = 0 or another value in case of error.
20# Check if this script is running as 'Administrator'
21$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
22$as_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
25 Write-Host "$([char]27)[92mSUCCESS This script has been started with 'Administrator' privileges.$([char]27)[0m"
27 Write-Host "$([char]27)[92mSUCCESS This script has been started with 'User' privileges.$([char]27)[0m"
30# Get/Check 'PYENV_ROOT' environment variable
32 $pyenv_root = [System.Environment]::GetEnvironmentVariable('PYENV_ROOT').Trim()
35 Write-Host "$([char]27)[91mERROR Cannot find 'PYENV_ROOT' environment variable. 'pyenv' is missing (RC = $rc).$([char]27)[0m"
36 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
39Write-Host "$([char]27)[92mSUCCESS 'pyenv' is installed at: '$pyenv_root'$([char]27)[0m"
41# Scan for 'pyenv' in 'Machine' PATH
42$machine_paths = [System.Environment]::GetEnvironmentVariable('PATH', "Machine") -Split ";"
43$found_machine_bin = ""
44$found_machine_shims = ""
45foreach ($item in $machine_paths) {
48 if ($item1 -eq ($pyenv_root + "bin")) {
49 $found_machine_bin = $item1
51 if ($item1 -eq ($pyenv_root + "shims")) {
52 $found_machine_shims = $item1
56Write-Host "$([char]27)[37mINFO 'pyenv' 'bin' on 'Machine' PATH: '$found_machine_bin'$([char]27)[0m"
57Write-Host "$([char]27)[37mINFO 'pyenv' 'shims' on 'Machine' PATH: '$found_machine_shims'$([char]27)[0m"
59# Scan for 'pyenv' in 'User' PATH
60$user_paths = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";"
63foreach ($item in $user_paths) {
66 if ($item1 -eq ($pyenv_root + "bin")) {
67 $found_user_bin = $item1
69 if ($item1 -eq ($pyenv_root + "shims")) {
70 $found_user_shims = $item1
74Write-Host "$([char]27)[37mINFO 'pyenv' 'bin' on 'User' PATH: '$found_user_bin'$([char]27)[0m"
75Write-Host "$([char]27)[37mINFO 'pyenv' 'shims' on 'User' PATH: '$found_user_shims'$([char]27)[0m"
77# Check if 'pyenv' is installed for 'All Users' or for 'This User Only'
78if (($found_machine_bin -ne "") -and ($found_machine_shims -ne "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
79 Write-Host "$([char]27)[92mSUCCESS 'pyenv' PATH items found in 'Machine' PATH for 'All Users'.$([char]27)[0m"
80} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -ne "") -and ($found_user_shims -ne "")) {
81 Write-Host "$([char]27)[92mSUCCESS 'pyenv' PATH items found in 'User' PATH for 'This User Only'.$([char]27)[0m"
82} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
83 # Not any 'pyenv' PATH item found
85 Write-Host "$([char]27)[91mERROR Cannot find 'pyenv' PATH items (RC = $rc).$([char]27)[0m"
86 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
89 # Otherwise, the 'pyenv' PATH definition is inconsistent
91 Write-Host "$([char]27)[91mERROR Inconsistent 'pyenv' PATH definition detected (RC = $rc).$([char]27)[0m"
92 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
93 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
97# Try to find 'pyenv' Python executable without calling Python
98Write-Host "$([char]27)[37mINFO Trying to find 'pyenv' Python executable by command 'where.exe python':$([char]27)[0m"
99where.exe python | Tee-Object -Variable output
100$output_lines = $output -Split "\r\n"
103foreach ($line in $output_lines) {
104 $line1 = $line.Trim()
105 if ($line.StartsWith($pyenv_root)) {
106 # Detect 'pyenv' Python executable
107 $found_python = $line1
110 # Detect a conflict with another global Python executable,
111 # which have a higher PATH priority then 'Pyenv'.
112 $conflicts += @(, $line1)
115if ($found_python -eq "") {
116 Write-Host "$([char]27)[91mERROR Unexpectedly cannot find Python executable.$([char]27)[0m"
117 Write-Host "$([char]27)[37mINFO Install/repair/configure 'pyenv'. Then try again.$([char]27)[0m"
119 if ($conflicts.count -eq 0) {
120 Write-Host "$([char]27)[92mSUCCESS Python executable found at '$found_python' (RC = $rc).$([char]27)[0m"
123 Write-Host "$([char]27)[91mERROR Found path conflicts (RC = $rc):$([char]27)[0m"
124 foreach ($conflict in $conflicts) {
125 Write-Host "$([char]27)[91mERROR * '$conflict'.$([char]27)[0m"
127 Write-Host "$([char]27)[95mNOTICE It is your decision, how to manage your PATH. This program cannot do this for you.$([char]27)[0m"
128 Write-Host "$([char]27)[95mNOTICE To get help, completely read the unit 'Path Conflicts' in the documentation.$([char]27)[0m"
129 Write-Host "$([char]27)[37mINFO Resolve the conflicts adopting the PATH on 'Machine' and 'User' according to your needs. Then try again.$([char]27)[0m"
138# --- END OF CODE ------------------------------------------------------