Skip to content

Instantly share code, notes, and snippets.

@urish
Created April 7, 2024 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urish/f51f66a851c127a4a9a66f7486fdbe51 to your computer and use it in GitHub Desktop.
Save urish/f51f66a851c127a4a9a66f7486fdbe51 to your computer and use it in GitHub Desktop.
Node.js script to remove duplicate path entries on Windows 11
// The script removes duplicate path entries from the User's path.
// It prints a power shell command that you should execute in order to update
// the User's path with the new value.
const { execSync } = require('child_process');
const machinePath = execSync('powershell.exe [Environment]::GetEnvironmentVariable(\\"Path\\", [EnvironmentVariableTarget]::Machine)').toString();
const userPath = execSync('powershell.exe [Environment]::GetEnvironmentVariable(\\"Path\\", [EnvironmentVariableTarget]::User)').toString();
const machinePathParts = machinePath.split(';');
const userPathParts = userPath.split(';');
const uniqueUserPaths = Array.from(new Set(userPathParts)).filter(path => !machinePathParts.includes(path));
const newPath = uniqueUserPaths.join(';');
console.log(`
[Environment]::SetEnvironmentVariable(
"Path",
"${newPath}",
[EnvironmentVariableTarget]::User)
`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment