1# Windows PowerShell Script to create a
2# virtual environment directory junction.
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 has 2 command line arguments:
15# 1. Absolute/relative path to the junction.
16# 2. Absolute/relative path to its target.
18# It will be called by 'pyenv-virtualenv' its parent folder.
20# The script returns RC = 0 or another value in case of error.
23# Check if this script is running as 'Administrator'
24$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
25$as_admin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
27# Get/Check argument 1 (path)
29 $link_path = $args[0].Trim()
32 Write-Host "$([char]27)[91mERROR Missing argument 1 (path) (RC = $rc).$([char]27)[0m"
33 Write-Host "$([char]27)[37mINFO To define a directory junction, assign 2 arguments 'path' and 'target' as absolute paths.$([char]27)[0m"
37# Get/Check argument 2 (target)
39 $link_target = $args[1].Trim()
42 Write-Host "$([char]27)[91mERROR Missing argument 2 (target) (RC = $rc).$([char]27)[0m"
43 Write-Host "$([char]27)[37mINFO To define a directory junction, assign 2 arguments 'path' and 'target' as absolute paths.$([char]27)[0m"
47# Make symbolic link to patched 'pyenv.bat'
49 Write-Host "$([char]27)[37mINFO Writing directory junction: $([char]27)[0m"
50 Write-Host "$([char]27)[37mINFO * Path: '$link_path'.$([char]27)[0m"
51 Write-Host "$([char]27)[37mINFO * Target: '$link_target'.$([char]27)[0m"
52 Write-Host "$([char]27)[37mINFO This could take some seconds ...$([char]27)[0m"
54 New-Item -ItemType Junction -Path "$link_path" -Target "$link_target" | Out-Null
57 Write-Host "$([char]27)[91mERROR Unexpectedly cannot write junction (RC = $rc).$([char]27)[0m"
58 Write-Host "$([char]27)[37mINFO Analyze/configure/repair the situation, why a script running as 'Administrator' fails. Then try again.$([char]27)[0m"
61 Write-Host "$([char]27)[37mINFO Done.$([char]27)[0m"
64 Write-Host "$([char]27)[91mERROR Insufficient privileges. (RC = $rc).$([char]27)[0m"
65 Write-Host "$([char]27)[37mINFO To create a junction, you must call this script in a console terminal with 'Administrator' privileges. Then try again.$([char]27)[0m"
73# --- END OF CODE ------------------------------------------------------