Mike Slinn

Add To Windows User Path

Published 2025-12-17.
Time to read: 1 minutes.

This page is part of the posts collection, categorized under Windows.

This article presents a PowerShell script that adds a directory to the current users Windows path. The script is not limited to 1024-character PATHs.

Motivation

I wanted to add the directory containing the script to the Windows PATH. However, the normal method (setx) truncates PATH to 1024 characters. I wonder why Microsoft has not removed this limitation.

Windows has some really old code from the dawning of PCs. The Windows setx command was first released in 1999 as part of the Windows 2000 Professional Resource Kit.

AddToPath.ps1

Enable PowerShell script executions:

PowerShell (Administrative)
PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

The AddToPath.ps1 PowerShellscript adds to the user PATH without using SETX.

AddToPath.ps1
# Mike Slinn mslinn@mslinn.com
# Adds the specified directory to the user PATH

param (
    [Parameter(Mandatory=$true, Position=0, HelpMessage="Enter the full folder path to add to your PATH.")]
    [string]$PathToAdd
)

# 1. Standardize the path (ensures folder exists and cleans up formatting)
try {
    $PathToAdd = (Resolve-Path $PathToAdd -ErrorAction Stop).Path
} catch {
    Write-Error "The directory '$PathToAdd' does not exist. Please check the path and try again."
    return
}

# 2. Fetch current User PATH
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")

# 3. Split into array and filter out empty entries
$pathArray = $userPath -split ';' | Where-Object { $_ -ne "" }

# 4. Check for duplicates
if ($pathArray -contains $PathToAdd) {
    Write-Host "The path '$PathToAdd' is already in your User PATH." -ForegroundColor Yellow
} else {
    # 5. Append and Save via .NET (Safe for strings > 1024 chars)
    $newPath = ($pathArray + $PathToAdd) -join ';'
    [Environment]::SetEnvironmentVariable("Path", $newPath, "User")

    Write-Host "Successfully added to User PATH: $PathToAdd" -ForegroundColor Green
    Write-Host "Restart your terminal to apply changes." -ForegroundColor Cyan
}

I saved the PowerShell script to F:\work\mslinn_bin\misc\AddToPath.ps1. The F:\work\mslinn_bin\misc folder itself needed to be added to the Windows user PATH. The script does not need to run as Administrator:

PowerShell
PS C:\Users\Mike Slinn> F:\work\mslinn_bin\misc\AddToPath.ps1 "F:\work\mslinn_bin\misc"
Successfully added to User PATH: F:\work\mslinn_bin\misc
Restart your terminal to apply changes. 
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.