PowerShell修改环境变量

如果你搜索本问题,多半会来到StackOverflow中看到这个回答

setx /M PATH "%PATH%;<your-new-path>"

Note: CMD中执行

正如该回答的某个回复提到的:

DO NOT USE THIS. setx will truncate PATH to 1024 characters. If PATH was changed before by being prepended with more directories, you will lose most of the original PATH contents!

这个会有1024个字符限制,如果你原有的%PATH%很长会被自动截断。

所以还是老实地调用.NET Framework的方法吧

$newPath = 'F:\common tools\git\bin;F:\common tools\python\app;F:\common tools\python\app\scripts;F:\common tools\ruby\bin;F:\masm32\bin;F:\Borland\BCC55\Bin'
$oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
[Environment]::SetEnvironmentVariable('PATH', "$newPath;$oldPath",'Machine');

Note: PowerShell中执行

posted @ 2022-11-07 13:54  talentzemin  阅读(109)  评论(0编辑  收藏  举报