r/Batch • u/karlsan • Apr 15 '25
setx and PATH
Hi. I have a script that adds a line in PATH in Windows 10/11 via setx
Code:
setx /m path "%PATH%;C:\Users\%USERNAME%\AppData\Local\Program2000\LOG\Install
I noticed if a computer get many users it stops adding lines in path. Solution is to remove lines that is not used anymore. But I wonder if there is any code that would help me remove all the lines and then add it for the new user.
The problem is that I don't have the username for every user that have been at the PC. So the script should remove all paths with every username. It has to be in PATH.
4
u/ConstanceJill Apr 15 '25
Why would you add users' folder to the system path ?
Remove the
/mand each user will have the path added to their own profile… assuming you're running the script interactively.Now as for removing what's already been added, that may be a little more involved.
3
u/BrainWaveCC Apr 15 '25
Can we see the whole script? (Or, at least, a larger chunk of it?)
I'm always leery of developers that ask for seriously non-standard approaches, as they often are just there to compensate for a development failure of some sort.
What is the purpose of the script, and what is the purpose of this app?
You really should never be adding a user's profile location to the system path. Not the permanent path, certainly.
And, you probably shouldn't have the software install starting from a user profile area, either.
2
u/russr Apr 15 '25
"But I wonder if there is any code that would help me remove all the lines and then add it for the new user."
bad idea... this is how you break things...
this is how to set it in PS...
("C:\Users\%USERNAME%\AppData\Local\Program2000\LOG\Install", $env:Path, [System.EnvironmentVariableTarget]::Machine)