Created
April 7, 2024 10:18
-
-
Save urish/f51f66a851c127a4a9a66f7486fdbe51 to your computer and use it in GitHub Desktop.
Node.js script to remove duplicate path entries on Windows 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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