To add to Justin's answer, βMachineβ may not be the goal you are looking for here, even though snipet code is working.
First of all, if PATHEXT no longer contains ".BAT" or ".CMD", the problem is elsewhere.
If this is the correct behavior in your environment, then read it.
$pathext = [Environment]::GetEnvironmentVariable("PATHEXT", "Process") [Environment]::SetEnvironmentVariable("PATHEXT", $pathext+';.BAT', "Process")
- The target "Machine" places the variable in the "HKEY_LOCAL_MACHINE" registry, ready for the following processes to read
- The User target places the variable in the HKEY_CURRENT_USER registry, ready to read for the next process
- The Process target puts the variable in operational state right now for the current PowerShell.exe process.
When I say "process", I really mean a different executable file launched by other means than your script. The processes started by your script command (like your bat / cmd command) are child processes and inherit the current environment, so all defined variables, unless you specify it otherwise than the "initial process" of the second thread.
What I can read for is what you are trying to achieve. the PATHEXT variable with the newly added content will only work through the PowerShell script / process and will be available to your bat / cmd script.
source share