Published 2025-12-17.
Time to read: 1 minutes.
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:
PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
The AddToPath.ps1 PowerShellscript adds to the user
PATH without using SETX.
# 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:
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.