about_PowerShell_Config

簡短描述

PowerShell 的組態檔,取代登錄組態。

詳細描述

檔案 powershell.config.json 包含PowerShell的組態設定。 PowerShell 會在啟動時載入此設定。 您也可以在執行時間修改設定。 先前,這些設定會儲存在適用於PowerShell的 Windows 登錄中,但現在包含在檔案中,以在macOS和Linux上啟用設定。

設定摘要

檔案 powershell.config.json 可以包含下列索引鍵:

  • DisableImplicitWinCompat
  • WindowsPowerShellCompatibilityModuleDenyList
  • WindowsPowerShellCompatibilityNoClobberModuleList
  • ExperimentalFeatures
  • LogChannels
  • LogIdentity
  • LogKeywords
  • LogLevel
  • Microsoft.PowerShell:ExecutionPolicy
  • PSModulePath
  • PowerShellPolicies
    • ExecutionPolicy
    • ConsoleSessionConfiguration
    • ModuleLogging
    • ProtectedEventLogging
    • ScriptBlockLogging
    • ScriptExecution
    • Transcription
    • UpdatableHelp

並非所有金鑰都適用於所有平臺。 機 PowerShellPolicies 碼包含可鏡像視窗組策略所管理設定的子機碼。 在 JSON 檔案的根層級定義時,這些子機碼也適用於所有平臺。

警告

組態檔中無法辨識的索引鍵或無效值會被忽略。 powershell.config.json如果檔案包含無效的 JSON,PowerShell 就無法啟動互動式會話。 如果發生這種情況,您必須修正組態檔。

組態範圍

您可以為所有使用者或個別使用者層級定義組態設定。

AllUsers (共用) 組態

powershell.config.json目錄中的$PSHOME檔案會定義從該 PowerShell 安裝執行之所有 PowerShell 工作階段的組態。

注意

位置 $PSHOME 定義為與執行System.Management.Automation.dll元件相同的目錄。 這也適用於裝載的PowerShell SDK實例。

CurrentUser (每位使用者) 組態

您也可以將檔案放在用戶範圍組態目錄中,以個別用戶為基礎設定PowerShell。 您可以使用 命令 Split-Path $PROFILE.CurrentUserCurrentHost,跨平臺找到使用者組態目錄。

範圍優先順序

在 Windows 上,由 Windows 組策略管理的設定優先於組態檔中的設定。 組策略不存在於非 Windows 平臺上。

組策略之後,在 AllUsers 層級定義的設定優先於針對 CurrentUser 層級定義的設定。

Windows 特定設定

下列設定僅適用於 Windows 平臺。

  • DisableImplicitWinCompat
  • WindowsPowerShellCompatibilityModuleDenyList
  • WindowsPowerShellCompatibilityNoClobberModuleList
  • ExecutionPolicy
  • PowerShellPolicies

DisableImplicitWinCompat

當設定為 true時,此設定會停用 Windows PowerShell 相容性功能。 Windows PowerShell 兼容性可讓 PowerShell 7 在相容性模式中載入 Windows PowerShell 5.1 模組。

如需詳細資訊,請參閱 about_Windows_PowerShell_Compatibility

WindowsPowerShellCompatibilityModuleDenyList

此設定是您想要排除而無法參與 Windows PowerShell 相容性功能的模組名稱陣列。

如需詳細資訊,請參閱 about_Windows_PowerShell_Compatibility

WindowsPowerShellCompatibilityNoClobberModuleList

此設定是模組名稱的陣列,載入模組的 Windows PowerShell 5.1 版本,不應進行 clobber。

如需詳細資訊,請參閱 about_Windows_PowerShell_Compatibility

ExecutionPolicy

重要

此設定僅適用於 Windows 平臺。

設定 PowerShell 會話的執行原則,判斷可執行哪些腳本。 根據預設,PowerShell 會使用現有的執行原則。

針對 AllUsers 組態,這會設定 LocalMachine 執行原則。 針對 CurrentUser 組態,這會設定 CurrentUser 執行原則。

下列範例會將 PowerShell 的執行原則設定為 RemoteSigned

{
  "Microsoft.PowerShell:ExecutionPolicy": "RemoteSigned"
}

如需詳細資訊,請參閱 about_Execution_Policies

PowerShellPolicies

Windows 有數個可由組策略管理的設定。 這些設定通常會儲存在 Windows 登錄中。 這些設定也可以在檔案中 powershell.config.json 定義。

PowerShellPolicies是 JSON 物件,其中包含各種原則設定的索引鍵/值組。 這些原則設定也可以列在物件外部 PowerShellPolicies JSON 檔案的根層級。 這個設定可以包含下列子機碼:

  • ConsoleSessionConfiguration
  • ModuleLogging
  • ProtectedEventLogging
  • ScriptBlockLogging
  • ScriptExecution
  • Transcription
  • UpdatableHelp

ScriptExecution 設定是用來設定PowerShell執行原則。 這會優先於 ExecutionPolicy 上述設定。

範例:

{
    "PowerShellPolicies": {
        "ScriptExecution": {
            "ExecutionPolicy": "RemoteSigned"
        }
    }
}

如需其他原則設定的描述,請參閱一般組態設定一節中的描述。

在 Windows 上,PowerShell 會尋找登錄中的設定。 登錄中找到的任何設定都有優先順序。 接下來 PowerShell 會讀取 JSON 設定。 在下 PowerShellPolicies找到且未在登錄中定義的任何設定,都優先於 JSON 組態根層級上找到的設定。

如需詳細資訊,請參閱 about_Group_Policy_Settings

非 Windows 平臺的 設定

下列設定僅適用於Linux和macOS平臺。

下列金鑰可用來設定適用於 Linux 和 macOS 的 PowerShell 記錄。

  • LogChannels
  • LogIdentity
  • LogKeywords
  • LogLevel

如需非 Windows 系統之 PowerShell 記錄的完整描述,請參閱 about_Logging_Non-Windows

一般組態設定

下列設定適用於所有支持的平臺。

  • ConsoleSessionConfiguration
  • ExperimentalFeatures
  • ModuleLogging
  • ProtectedEventLogging
  • PSModulePath
  • ScriptBlockLogging
  • ScriptExecution
  • Transcription
  • UpdatableHelp

ConsoleSessionConfiguration

此設定會指定要用於所有 PowerShell 工作階段的工作階段組態。 這可以是本機計算機上註冊的任何端點,包括預設PowerShell遠端端點或具有特定使用者角色功能的自定義端點。

此機碼包含兩個子機碼:

  • EnableConsoleSessionConfiguration - 若要啟用工作階段群組態,請將 值設定為 true。 根據預設,此值為 false

  • ConsoleSessionConfigurationName - 指定執行 PowerShell 的組態端點名稱。 根據預設,不會定義任何會話。

{
  "ConsoleSessionConfiguration": {
    "EnableConsoleSessionConfiguration": false,
    "ConsoleSessionConfigurationName" : []
  }
}

如需詳細資訊,請參閱 about_Session_Configurations

ExperimentalFeatures

在 PowerShell 中啟用的實驗性功能名稱。 預設值為空陣列。

下列範例會在 PowerShell 啟動時啟用 PSCommandNotFoundSuggestionPSSubsystemPluginModel 實驗功能。

範例:

{
  "ExperimentalFeatures": [
    "PSCommandNotFoundSuggestion",
    "PSSubsystemPluginModel"
  ]
}

如需實驗性功能的詳細資訊,請參閱 使用實驗性功能

ModuleLogging

此設定會控制PowerShell模組的記錄行為。 這個設定包含兩個子機碼:

  • EnableModuleLogging - 若要啟用工作階段群組態,請將 值設定為 true。 啟用時,會將指定模組成員的管線執行事件記錄在PowerShell記錄檔中。
  • ModuleNames - 指定應該記錄的模組名稱。

範例:

{
  "ModuleLogging": {
    "EnableModuleLogging": true,
    "ModuleNames" : [
        "PSReadLine",
        "PowerShellGet"
    ]
  }
}

ProtectedEventLogging

此設定可讓您設定受保護的事件記錄。 這個設定包含兩個子機碼:

  • EnableProtectedEventLogging - 如果您啟用此原則設定,則支援它的元件會使用您提供的憑證來加密記錄數據,再將它寫入記錄檔。 數據會使用密碼編譯訊息語法 (CMS) 標準進行加密。 如果您有憑證私鑰的存取權,您可以使用 Unprotect-CmsMessage 來解密這些加密的訊息。
  • EncryptionCertificate - 提供要用於加密的憑證名稱清單。

範例:

{
  "ProtectedEventLogging": {
    "EnableProtectedEventLogging": false,
    "EncryptionCertificate": [
      "Joe"
    ]
  }
}

PSModulePath

PSModulePath 寫此 PowerShell 工作階段的設定。 如果組態是目前使用者的,請設定 CurrentUser 模組路徑。 如果設定適用於所有使用者,請設定 AllUsers 模組路徑。

警告

在這裡設定 AllUsersCurrentUser 模組路徑並不會變更 PowerShellGet Cmdlet 的範圍安裝位置,例如 Install-Module。 這些 Cmdlet 一律使用 預設 模組路徑。

如果未設定任何值,PowerShell 會使用個別模塊路徑設定的預設值。 如需這些預設值的詳細資訊,請參閱 about_PSModulePath

此設定可讓環境變數在字元之間 % 內嵌,例如 "%HOME%\Documents\PowerShell\Modules",就像 Windows 命令殼層允許的方式一樣。 此語法也適用於Linux和macOS。 如需範例,請參閱下方。

這個範例顯示 PSModulePath Windows 環境的設定:

{
  "PSModulePath": "C:\\Program Files\\PowerShell\\6\\Modules"
}

此範例顯示 PSModulePath macOS 或 Linux 環境的組態:

{
  "PSModulePath": "/opt/powershell/6/Modules"
}

此範例示範在組態中 PSModulePath 內嵌環境變數。 請注意,使用 HOME 環境變數和 / 目錄分隔符時,此語法適用於 Windows、macOS 和 Linux。

{
  "PSModulePath": "%HOME%/Documents/PowerShell/Modules"
}

此範例使用僅適用於 macOS 和 Linux 的環境變數:

{
  "PSModulePath": "%XDG_CONFIG_HOME%/powershell/Modules"
}

注意

PowerShell 變數無法內嵌在組態中 PSModulePathPSModulePath Linux 和macOS上的組態會區分大小寫。 組 PSModulePath 態必須針對平臺使用有效的目錄分隔符。 在macOS和Linux上,這表示 /。 在 Windows 上,和 \ 都能夠/運作。

ScriptBlockLogging

此設定可控制所有 PowerShell 腳本輸入的記錄。 這個設定包含兩個子機碼:

  • EnableScriptBlockLogging - 如果您啟用此原則設定,PowerShell 會記錄命令、腳本區塊、函式和腳本的處理,無論是以互動方式叫用,還是透過自動化來叫用。
  • EnableScriptBlockInvocationLogging - 啟用文稿區塊啟動和停止事件的記錄。

範例:

"ScriptBlockLogging": {
  "EnableScriptBlockInvocationLogging": true,
  "EnableScriptBlockLogging": false
}

文字記錄

此原則設定可讓您在文字型文字記錄中擷取 PowerShell 命令的輸入和輸出。 如果您啟用此原則設定,PowerShell 會啟用所有 PowerShell 工作階段的轉譯。

此設定可控制轉譯在PowerShell中的運作方式。 此設定包含三個子機碼:

  • EnableTranscripting - 啟用此設定時,PowerShell 會在設定的位置中建立轉譯記錄檔。
  • EnableInvocationHeader - 根據預設,PowerShell 會在轉譯記錄檔頂端包含標頭。 您可以使用此設定來停用標頭。
  • OutputDirectory - 此設定可讓您收集中央位置的轉譯記錄檔,而不是預設位置。

範例:

{
    "Transcription": {
        "EnableTranscripting": true,
        "EnableInvocationHeader": true,
        "OutputDirectory": "c:\\tmp"
      }
}

如需詳細資訊,請參閱 開始文字記錄

UpdateableHelp

此原則設定可讓您在 Cmdlet 上設定 SourcePath 參數的Update-Help預設值。 您可以使用SourcePath參數指定不同的值來覆寫這個預設值。

範例:

{
    "UpdatableHelp": {
      "DefaultSourcePath": "f:\\temp"
    }
}