Search code examples
windowspathenvironment-variablessymlink

Win10: Using symbolic links in the PATH variable


Here's the deal. I want to use a symbolic link to the latest installed Java version and create a symbolic link. The PATH variable shall contain the symbolic link as entry. The link is not resolved, though. Why?

Example:

$> dir C:\Program\ Files\Java
C:\Program\ Files\Java\jdk-1.14.1\
C:\Program\ Files\Java\jdk-latest.lnk  # link to jdk-1.14.1

$> echo %PATH%
#...
C:\Program\ Files\Java\jdk-latest\bin

$> java --version
The command "java" is either written wrong or couldn't be found.

Solution

  • New-Item cmdlet with -ItemType SymbolicLink can be used to create symbolic link to file or folder.

    Suppose, you have 2 folders containing 2 different versions of nodejs

    node-v14.18.0-win-x64
    node-v14.20.0-win-x64
    

    We need one symbolic link, say rel which will point to one of the nodejs folders.

    Add C:\Program\Files\nodejs\rel to PATH environment variable

    Point rel to node v14.18.0

    New-Item -Type SymbolicLink -Path .\rel -Target .\node-v14.18.0-win-x64 -Force
    

    Verify node version

    PS C:\Users\pvaddepa> node -v
    v14.18.0
    

    Use -Force to update the symbolic link to point to node v14.20.0.

    New-Item -Type SymbolicLink -Path .\rel -Target .\node-v14.20.0-win-x64 -Force
    

    Verify the node version

    PS C:\Users\pvaddepa> node -v
    v14.20.0
    

    See New-Item example .