嘗試在 Windows PowerShell 中設定環境變數

嘗試在 Windows PowerShell 中設定環境變數

我正在嘗試在 Windows 10 上的 PowerShell 中設定%systemroot%環境變數。

這是我嘗試過的方法和結果:

$env:SystemRoot = "C:\Windows"
echo %SystemRoot%
%SystemRoot%

setx SystemRoot "C:\Windows"
SUCCESS: Specified value was saved.
echo %SystemRoot%
%SystemRoot%

我究竟做錯了什麼?

答案1

您混淆了 PowerShell 和 DOS 語法。 %SystemRoot%是 DOS 語法,PowerShell 語法是$env:SystemRoot.

下面的例子也許可以闡明其中的差異:

在此輸入影像描述

請注意,值的變更SystemRoot僅適用於這一個 PowerShell 實例(不是系統範圍的)。

參考: 如何在 PowerShell 中使用環境變數

相關內容