windows設(shè)置環(huán)境變量的5種方法

01.gui操作

這是最常用的,我之前一直用的是這一種

在此電腦右鍵,屬性,高級系統(tǒng)設(shè)置,環(huán)境變量。。。

02.set 命令

和linux系統(tǒng)不一樣,linux里面是可以把命令配到bashrc之類的文件里,這樣環(huán)境變量就一直生效了。

set命令,只在這個(gè)終端內(nèi)生效,退出就失效。

雖然也可以像linux一樣把命令放到$profile里面,但是我覺得比較蠢,因?yàn)檫@樣只有powershell終端里面生效,換成cmd是不會(huì)加載你這個(gè)profile的。

這個(gè)和powershell里面直接修改變量是一樣的

像下面這樣操作,當(dāng)前窗口,搜索命令的Path變量就添加了。

$env:Path + ";path/your/new"

03.使用reg文件

04 setx命令

屬于是很老的命令了

基本用法

setx 變量名稱  變量值

默認(rèn)改的是用戶環(huán)境變量,尾部添加/m,可以修改系統(tǒng)環(huán)境變量。

例子:jdk版本管理

因?yàn)閖dk是通過JAVA_HOME變量配置的,所以可以下載多個(gè)jdk,然后用setx設(shè)置環(huán)境變量來切換

這里用到了gsudo這個(gè)軟件,比較方便,沒有裝這個(gè)軟件需要打開管理員的終端執(zhí)行命令

#安裝jdk11
sudo choco install microsoftopenjdk11
#上面安裝完會(huì)提示安裝jdk的位置,這里我們賦值之后設(shè)置JAVA_HOME,設(shè)置到系統(tǒng)變量。
sudo setx JAVA_HOME C:\Program Files\Microsoft\jdk-11.0.18.10-hotspot /m
#順便把bin目錄加到path
sudo setx Path ($env:Path +';C:\Program Files\Microsoft\jdk-11.0.18.10-hotspot')
? setx  /?

SetX has three ways of working:

Syntax 1:
    SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]

Syntax 2:
    SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]

Syntax 3:
    SETX [/S system [/U [domain\]user [/P [password]]]]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]

Description:
    Creates or modifies environment variables in the user or system
    environment. Can set variables based on arguments, regkeys or
    file input.

Parameter List:
    /S     system          Specifies the remote system to connect to.

    /U     [domain\]user   Specifies the user context under which
                           the command should execute.

    /P     [password]      Specifies the password for the given
                           user context. Prompts for input if omitted.

    var                    Specifies the environment variable to set.

    value                  Specifies a value to be assigned to the
                           environment variable.

    /K     regpath         Specifies that the variable is set based
                           on information from a registry key.
                           Path should be specified in the format of
                           hive\key\...\value. For example,
                           HKEY_LOCAL_MACHINE\System\CurrentControlSet\
                           Control\TimeZoneInformation\StandardName.

    /F     file            Specifies the filename of the text file
                           to use.

    /A     x,y             Specifies absolute file coordinates
                           (line X, item Y) as parameters to search
                           within the file.

    /R     x,y string      Specifies relative file coordinates with
                           respect to "string" as the search parameters.

    /M                     Specifies that the variable should be set in
                           the system wide (HKEY_LOCAL_MACHINE)
                           environment. The default is to set the
                           variable under the HKEY_CURRENT_USER
                           environment.

    /X                     Displays file contents with x,y coordinates.

    /D     delimiters      Specifies additional delimiters such as ","
                           or "\". The built-in delimiters are space,
                           tab, carriage return, and linefeed. Any
                           ASCII character can be used as an additional
                           delimiter. The maximum number of delimiters,
                           including the built-in delimiters, is 15.

    /?                     Displays this help message.

NOTE: 1) SETX writes variables to the master environment in the registry.

      2) On a local system, variables created or modified by this tool
         will be available in future command windows but not in the
         current CMD.exe command window.

      3) On a remote system, variables created or modified by this tool
         will be available at the next logon session.

      4) The valid Registry Key data types are REG_DWORD, REG_EXPAND_SZ,
         REG_SZ, REG_MULTI_SZ.

      5) Supported hives:  HKEY_LOCAL_MACHINE (HKLM),
         HKEY_CURRENT_USER (HKCU).

      6) Delimiters are case sensitive.

      7) REG_DWORD values are extracted from the registry in decimal
         format.

Examples:
    SETX MACHINE COMPAQ
    SETX MACHINE "COMPAQ COMPUTER" /M
    SETX MYPATH "%PATH%"
    SETX MYPATH ~PATH~
    SETX /S system /U user /P password  MACHINE COMPAQ
    SETX /S system /U user /P password MYPATH ^%PATH^%
    SETX TZONE /K HKEY_LOCAL_MACHINE\System\CurrentControlSet\
         Control\TimeZoneInformation\StandardName
    SETX BUILD /K "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows
         NT\CurrentVersion\CurrentBuildNumber" /M
    SETX /S system /U user /P password TZONE /K HKEY_LOCAL_MACHINE\
         System\CurrentControlSet\Control\TimeZoneInformation\
         StandardName
    SETX /S system /U user /P password  BUILD /K
         "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\
         CurrentVersion\CurrentBuildNumber" /M
    SETX /F ipconfig.out /X
    SETX IPADDR /F ipconfig.out /A 5,11
    SETX OCTET1 /F ipconfig.out /A 5,3 /D "#$*."
    SETX IPGATEWAY /F ipconfig.out /R 0,7 Gateway
    SETX /S system /U user /P password  /F c:\ipconfig.out /X

Path變量設(shè)置

setx有一個(gè)問題是設(shè)置的最大長度是1024

所以path很長的時(shí)候,就不能用這個(gè)方法了。

比如我執(zhí)行這個(gè)命令就會(huì)報(bào)錯(cuò)

sudo setx Path ($env:Path +';C:/Program Files/Microsoft/jdk-11.0.18.10-hotspot/bin') /m

WARNING: The data being saved is truncated to 1024 characters.

05.powershell調(diào)用.net

其中GetEnvironmentVariable,一個(gè)參數(shù)是變量名,第二個(gè)參數(shù)target是范圍,可以是 Process(當(dāng)前進(jìn)程),User(當(dāng)前用戶)或 Machine(本地計(jì)算機(jī))

# 獲取當(dāng)前的 path 值
$path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
# 添加新的路徑到 path 中
$path += ";C:\NewPath"
# 設(shè)置新的 path 值
[System.Environment]::SetEnvironmentVariable("Path",$path,"Machine")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容