1# Windows PowerShell Script to write the PATH item for
2# 'pyenv-virtualenv' for Windows.
7# © 2025 Michael Paul Korthals. All rights reserved.
8# For legal details see documentation.
12# This script is located in the 'pyenv-virtualenv' plugin root directory.
14# It will be called by 'install.bat' its parent folder.
16# The script returns RC = 0 or another value in case of error.
20# Silently 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)
24# Silently Get/Check 'PYENV_ROOT' environment variable
26 $pyenv_root = [System.Environment]::GetEnvironmentVariable('PYENV_ROOT').Trim()
29 Write-Host "$([char]27)[91mERROR Cannot find 'PYENV_ROOT' environment variable. 'pyenv' is missing (RC = $rc).$([char]27)[0m"
30 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
34# Silently scan for 'pyenv' and 'pyenv-virtualenv' in 'Machine' PATH
35$machine_paths = [System.Environment]::GetEnvironmentVariable('PATH', "Machine") -Split ";"
36$found_machine_bin = ""
37$found_machine_shims = ""
38$found_machine_plugin_shims = ""
39$index_machine_bin = -1
40$index_machine_shims = -1
41$index_machine_plugin_shims = -1
43foreach ($item in $machine_paths) {
47 if ($item1 -eq ($pyenv_root + "bin")) {
48 $found_machine_bin = $item1
49 $index_machine_bin = $i
51 if ($item1 -eq ($pyenv_root + "shims")) {
52 $found_machine_shims = $item1
53 $index_machine_shims = $i
55 if ($item1 -eq ($pyenv_root + "plugins\pyenv-virtualenv\shims")) {
56 $found_machine_plugin_shims = $item1
57 $index_machine_plugin_shims = $i
62# Silently scan for 'pyenv' and 'pyenv-virtualenv' in 'User' PATH
63$user_paths = [System.Environment]::GetEnvironmentVariable('PATH', "User") -Split ";"
66$found_user_plugin_shims = ""
69$index_user_plugin_shims = -1
71foreach ($item in $user_paths) {
75 if ($item1 -eq ($pyenv_root + "bin")) {
76 $found_user_bin = $item1
79 if ($item1 -eq ($pyenv_root + "shims")) {
80 $found_user_shims = $item1
81 $index_user_shims = $i
83 if ($item1 -eq ($pyenv_root + "plugins\pyenv-virtualenv\shims")) {
84 $found_user_plugin_shims = $item1
85 $index_user_plugin_shims = $i
90# Check if 'pyenv' is installed for 'All Users' or for 'This User Only'
91if (($found_machine_bin -ne "") -and ($found_machine_shims -ne "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
92 # 'pyenv' PATH items found in 'Machine' PATH for 'All Users'
93 if ($found_machine_plugin_shims -eq "") {
95 $new_machine_path = $pyenv_root + "plugins\pyenv-virtualenv\shims"
96 foreach ($item in $machine_paths) {
97 $new_machine_path += ";" + $item
100 Write-Host "$([char]27)[37mINFO Writing registry. This could take some seconds ...$([char]27)[0m"
101 [System.Environment]::SetEnvironmentVariable('PATH', $new_machine_path, "Machine")
104 Write-Host "$([char]27)[91mERROR Cannot write 'Machine' PATH environment variable. (RC = $rc).$([char]27)[0m"
105 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
106 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
109 # 'pyenv-virtualenv' PATH item has been correctly written
110 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'Machine' PATH item is now in place. (RC = $rc).$([char]27)[0m"
111 Write-Host "$([char]27)[95mNOTICE Close and reopen the console terminal to let this change take effect.$([char]27)[0m"
114 Write-Host "$([char]27)[91mERROR Insufficient privileges. (RC = $rc).$([char]27)[0m"
115 Write-Host "$([char]27)[37mINFO Changing the 'Machine PATH' you should call this script in a console terminal with 'Administrator' privileges. Then try again.$([char]27)[0m"
116 Write-Host "$([char]27)[95mNOTICE Alternatively read the unit 'Path Conflicts'. Resolve/repair manually. Then try again.$([char]27)[0m"
120 if (($index_machine_plugin_shims -lt $index_machine_bin) -and ($index_machine_plugin_shims -lt $index_machine_shims)) {
121 # 'pyenv-virtualenv' PATH item is already set in place
122 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'shims' on 'Machine' PATH is already in place: '$found_machine_plugin_shims' (RC = $rc).$([char]27)[0m"
124 # 'pyenv-virtualenv' item in 'Machine' PATH
125 # has a lower priority than 'pyenv'.
127 Write-Host "$([char]27)[91mERROR 'pyenv-virtualenv' item in 'Machine' PATH has lower priority than 'pyenv' (RC = $rc).$([char]27)[0m"
128 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
129 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
133} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -ne "") -and ($found_user_shims -ne "")) {
134 # 'pyenv' PATH items found in 'User' PATH for 'This User Only'
135 if ($found_user_plugin_shims -eq "") {
136 $new_user_path = $pyenv_root + "plugins\pyenv-virtualenv\shims"
137 foreach ($item in $user_paths) {
138 $new_user_path += ";" + $item
141 Write-Host "$([char]27)[37mINFO Writing registry. This could take some seconds ...$([char]27)[0m"
142 [System.Environment]::SetEnvironmentVariable('PATH', $new_user_path, "User")
145 Write-Host "$([char]27)[91mERROR Cannot write 'User' PATH environment variable. (RC = $rc).$([char]27)[0m"
146 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
147 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
150 # 'pyenv-virtualenv' PATH item has been correctly written
151 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'User' PATH item is now in place. (RC = $rc).$([char]27)[0m"
152 Write-Host "$([char]27)[95mNOTICE Close and reopen the console terminal to let this change take effect.$([char]27)[0m"
154 if (($index_user_plugin_shims -lt $index_user_bin) -and ($index_user_plugin_shims -lt $index_user_shims)) {
155 # 'pyenv-virtualenv' PATH item is already set in place
156 Write-Host "$([char]27)[92mSUCCESS 'pyenv-virtualenv' 'shims' on 'User' PATH is already in place: '$found_user_plugin_shims' (RC = $rc).$([char]27)[0m"
158 # 'pyenv-virtualenv' item in 'User' PATH
159 # has a lower priority than 'pyenv'.
161 Write-Host "$([char]27)[91mERROR 'pyenv-virtualenv' item in 'User' PATH has lower priority than 'pyenv' (RC = $rc).$([char]27)[0m"
162 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
163 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
167} elseif (($found_machine_bin -eq "") -and ($found_machine_shims -eq "") -and ($found_user_bin -eq "") -and ($found_user_shims -eq "")) {
168 # Not any 'pyenv' PATH item found
170 Write-Host "$([char]27)[91mERROR Cannot find 'pyenv' PATH items (RC = $rc).$([char]27)[0m"
171 Write-Host "$([char]27)[37mINFO Install and configure 'pyenv'. Then try again.$([char]27)[0m"
174 # Otherwise, the 'pyenv' PATH definition is inconsistent
176 Write-Host "$([char]27)[91mERROR Inconsistent 'pyenv' PATH definition detected (RC = $rc).$([char]27)[0m"
177 Write-Host "$([char]27)[95mNOTICE Read the unit 'Path Conflicts' in the documentation to get help.$([char]27)[0m"
178 Write-Host "$([char]27)[37mINFO Repair the inconsistent 'pyenv' PATH definitions manually. Then try again.$([char]27)[0m"
186# --- END OF CODE ------------------------------------------------------