5

Chocolatey - PowerShell - Symlink

I installed a few packages using Chocolatey Package Manager. Rather than adding each of the installation directory to PATH or in my $PATH variable in PowerShell, I wish to create a directory and place symlinks to executables for each of the package. I did this for the following packages:

  • CCleaner
  • Google Chrome
  • Firefox
  • All of these worked fine. Recently I installed VSCode and VLC and the same method did not work. I used this command to create the symlinks in PowerShell:


    New-Item -ItemType SymbolicLink -Path "vlc.exe" -Value "C:\Program Files\VideoLAN\VLC\vlc.exe"
    VLC (and VSCode) both run fine when double clicked on the symlink. Is there a better way to do this (and why is this method not working)?

    8

    tl;dr: What you are attempting to do is not possible in the way you want to do it. Fortunately, ShimGenerator can do this exact thing (that's what it was designed for and it comes with Chocolatey): & "$env:ChocolateyInstall\tools\shimgen.exe" --output="$env:ChocolateyInstall\bin\vlc.exe" --path=" C:\Program Files\VideoLAN\VLC\vlc.exe" -iconpath="C:\Program Files\VideoLAN\VLC\vlc.exe"

    Shim created for VLC

    Why a Symbolic Link (Symlink) Won't Work

    Those executables have dependencies that they expect to be relative to the executable. You would need to symlink the file AND all of its dependent files/folders - so in other words you can't simply symlink the file, you would likely need to symlink the directory and put that directory on the PATH, which kind of defeats the purpose you are trying to achieve.

    Shimming is a Better Way

    Chocolatey creates shims when it finds executables in package directories. Symlinks fall down when the files have dependencies (as you are seeing).

    We've wrote more about it at https://chocolatey.org/docs/features-shim#benefits (emphasis mine):

    These are the benefits of creating a shim:

    • Provides an exe file that calls a target executable.
    • Runs the target executable where it is, which means all dependencies and other things used are all in the original location
    • When items require elevated privileges, shims will raise UAC prompts.
    • The exe can be called from powershell, bash, cmd.exe, or other shells just like you would call the target.
    • Blocks and waits for command line apps to finish running, exits immediately when running a GUI app.
    • Uses the icon of the target if the target exists on creation.
    • Works better than symlinks. Symlinks on Windows fall down at file dependencies. So if your file depends on other files and DLLs, all of those need to also be linked.
    • Does not require special privileges like creating symlinks (symbolic links) do. So you can create shims without administrative rights.

    If symlinks worked better, we would have probably used them instead of spending the amount of time we did working on shimgen. There is an FAQ on the shim feature page as well.

    Shimming in Chocolatey Packages

    Chocolatey automatically creates shims when executables are found in the package directory.

    You can also do this in chocolateyInstall.ps1 scripts (in the packaging) with Install-BinFile. See more details at https://chocolatey.org/docs/helpers-install-bin-file.

    How You Can Symlink VLC

    To fully symlink VLC, you would also need to symlink the following files and folders (and any subfolders and files in those folders) in addition to just vlc.exe:

    enter image description here

    While I could provide a script to symlink the entire thing, I don't think it is necessary given it is pretty much a futile approach.

    0

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.