解釋 PowerShell 配置檔案。
當您開啟 PowerShell 時,它會載入配置檔案,就像 Windows 作業系統一樣。當您登入到 Windows 作業系統時,您會登入到您的配置檔案,並且每個使用者都有自己的個人配置檔案。它被稱為當前主機的當前配置檔案。
要檢查您的配置檔案,請在 PowerShell 控制檯中鍵入$Profile 命令。
PS C:\Users\Administrator> $profile C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.p s1
這是針對 PowerShell 控制檯的,但讓我們檢查一下 PowerShell 是否對 ISE 使用相同的配置檔案。
PS C:\> $profile C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profil e.ps1
因此,ISE 也有自己的配置檔案,並且兩者都儲存在$Home 目錄中。如果我們對 VSCode 使用$profile 會怎麼樣。
PS C:\> $profile C:\Users\Administrator\Documents\PowerShell\Microsoft.VSCode_profile.ps1
這意味著每個編輯器都有自己的配置檔案,用於當前使用者和當前主機。
您可能已經注意到,每當您啟動 PowerShell 時,您都可以訪問系統上不同使用者建立的命令和模組,因為簡單地啟動 PowerShell 也會載入儲存在$PSHome 位置的模組。除了當前使用者之外
PS C:\> $pshome C:\Windows\System32\WindowsPowerShell\v1.0
上面的例子表明,也可能存在一個對所有使用者都存在的配置檔案。讓我們看看 PowerShell ISE 版本總共有多少個配置檔案。
PS C:\> $profile | fl * -Force AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Pow erShellISE_profile.ps1 CurrentUserAllHosts : C:\Users\Administrator\Documents\WindowsPowerShell\profi le.ps1 CurrentUserCurrentHost : C:\Users\Administrator\Documents\WindowsPowerShell\Micro soft.PowerShellISE_profile.ps1 Length : 86
上面的命令是從 ISE 執行的,我們現在將在 PowerShell 控制檯中檢查相同的命令,
PS C:\Users\Administrator> $PROFILE | fl * -Force AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Pow erShell_profile.ps1 CurrentUserAllHosts : C:\Users\Administrator\Documents\WindowsPowerShell\profi le.ps1 CurrentUserCurrentHost : C:\Users\Administrator\Documents\WindowsPowerShell\Micro soft.PowerShell_profile.ps1 Length : 83
當您比較以上兩個輸出時,您可以注意到當前主機(所有使用者和當前使用者)配置檔案取決於您使用的編輯器。如果您使用 PowerShell 控制檯,則配置檔名稱將包含 PowerShell 配置檔案,如果您使用 ISE 或 VSCode,則當前主機配置檔案將相應地包含名稱。
透過比較,我們得知配置檔案基本上儲存在兩個位置。1) $Home (C:\Users\<UserName>) 和 2) $PSHome (C:\Windows\System32\WindowsPowerShell)。
所以總共有 6 個配置檔案。
當前使用者,當前主機 - PowerShell 控制檯
當前使用者,所有主機
所有使用者,當前主機 - PowerShell 控制檯
所有使用者,所有主機
當前使用者,當前主機 - 取決於編輯器
所有使用者,當前主機 - 取決於編輯器。