Moving Chocolatey Packages with PowerShell

Last week, I built a package for Dynamsoft Barcode Reader using Chocolatey. One thing disappointed me that I can’t specify the installation directory if I don’t have a licensed Chocolatey edition. It means all packages will be installed to C:\ProgramData\chocolatey\lib. I do worry about my C drive storage spaces. Therefore, I decided to use PowerShell script moving Chocolatey packages. Although I’ve been a Windows user for many years, I never touched PowerShell before. What I’m going to do is to use PowerShell to move the package to a new location, remove the old package and register the destination directory to the environment path.

PowerShell Dev Tools

  • Visual Studio Code
  • Windows PowerShell ISE

How to Move Chocolatey Packages

To make the script work, we have to solve some problems.

How to get the PowerShell version?

PowerShell version is important because we need to check the corresponding documentation.


PowerShell version

How to run PowerShell scripts?

The first step is to start Windows PowerShell as administrator.

When running ps1 file at the first time, you may get the following error:

ps1 cannot be loaded because running scripts is disabled on this system.

The reason is the default execution policy is Restricted:

# Check the default policy

Change the policy to RemoteSigned:

Set-ExecutionPolicy RemoteSigned

How to combine multiple strings into a file path?

Use Join-Path to combine a path with a child path. If you want to create a path with multiple child paths, use pipeline:

$envPath = Join-Path $destinationDir -ChildPath $appName | Join-Path -ChildPath $binaryFolder

How to throw exceptions and terminate the script when we get some errors?

If there is an exception, the script will be automatically terminated, which is not user-friendly. We can use “try catch” to capture the exception, print the error information and then exit the process:

try {
    $destinationDir = Join-Path $partition -ChildPath "chocolatey" -ErrorAction Stop
catch {
    $exception = $_
    Write-Output $exception
    Exit 1

How to create a new directory?

If a directory does not exist, create it:

if (![System.IO.Directory]::Exists($destinationDir)) {

How to tell if a string contains a substring?

if ($environmentVariables.Contains($envPath)) {
        // TODO:

How to set environment variables?

According to the online tutorial, we can permanently update the environment path as follows:

$environmentVariables = [Environment]::GetEnvironmentVariable("Path", "Machine")
$environmentVariables = "$envPath;" + $environmentVariables
[Environment]::SetEnvironmentVariable("Path", $environmentVariables, "Machine")
  • Path” is the system environment variable.
  • $environmentVariables” is the value to be assigned to “Path”.
  • Machine” is the machine-level. Alternatively, you can use “User” and “Process”.

How to install and move a package?

Assume the package is Dynamsoft Barcode Reader:

choco install dbr

Move the package:


Move Chocolatey package with PowerShell

Source Code