共用方式為


Export-Console

將目前會話中的嵌入式管理單元名稱導出至主控台檔案。

語法

Export-Console
      [[-Path] <String>]
      [-Force]
      [-NoClobber]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

Description

Export-Console Cmdlet 會將目前會話中 Windows PowerShell 嵌入式管理單元的名稱導出至 Windows PowerShell 控制台檔案 (.psc1)。 您可以使用此 Cmdlet 來儲存嵌入式管理單元,以供日後會話使用。

若要將 .psc1 控制台檔案中的嵌入式管理單元新增至會話,請使用 Cmd.exe 或其他 Windows PowerShell 會話,在命令行啟動 Windows PowerShell (Powershell.exe),然後使用 Powershell.exe 的 PSConsoleFile 參數來指定控制台檔案。

如需 Windows PowerShell 嵌入式管理單元的詳細資訊,請參閱about_PSSnapins。

範例

範例 1:匯出目前會話中嵌入式管理單元的名稱

PS C:\> Export-Console -Path $pshome\Consoles\ConsoleS1.psc1

此命令會將目前會話中 Windows PowerShell 嵌入式管理單元的名稱匯出至 Windows PowerShell 安裝資料夾的 ConsoleS1.psc1 檔案,$pshome。

範例 2:將嵌入式管理單元的名稱匯出至最新的控制台檔案

PS C:\> Export-Console

此命令會將 Windows PowerShell 嵌入式管理單元的名稱從目前的工作階段匯出至目前工作階段中最近使用的 Windows PowerShell 控制台檔案。 它會覆寫先前的檔案內容。

如果您尚未在目前的會話期間匯出主控台檔案,系統會提示您繼續許可權,然後提示您輸入檔名。

範例 3:新增嵌入式管理單元並匯出嵌入式管理單元的名稱

PS C:\> Add-PSSnapin NewPSSnapin
PS C:\> Export-Console -path NewPSSnapinConsole.psc1
PS C:\> powershell.exe -PsConsoleFile NewPsSnapinConsole.psc1

這些命令會將 NewPSSnapin Windows PowerShell 嵌入式管理單元新增至目前的會話、將目前會話中的 Windows PowerShell 嵌入式管理單元名稱導出至控制台檔案,然後使用控制台檔案啟動 Windows PowerShell 會話。

第一個命令會使用 Add-PSSnapin Cmdlet,將 NewPSSnapin 嵌入式管理單元新增至目前的會話。 您只能新增已在系統上註冊的 Windows PowerShell 嵌入式管理單元。

第二個命令會將 Windows PowerShell 嵌入式管理單元名稱導出至 NewPSSnapinConsole.psc1 檔案。

第三個命令會使用 NewPSSnapinConsole.psc1 檔案啟動 Windows PowerShell。 因為主控台檔案包含 Windows PowerShell 嵌入式管理單元名稱,因此目前會話中提供嵌入式管理單元中的 Cmdlet 和提供者。

範例 4:將嵌入式管理單元的名稱匯出至指定的位置

PS C:\> export-console -path Console01
PS C:\> notepad console01.psc1
<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
  <PSVersion>2.0</PSVersion>
  <PSSnapIns>
     <PSSnapIn Name="NewPSSnapin" />
  </PSSnapIns>
</PSConsoleFile>

此命令會將目前會話中的 Windows PowerShell 嵌入式管理單元名稱匯出至目前目錄中的 Console01.psc1 檔案。

第二個命令會顯示 [記事本] 中 Console01.psc1 檔案的內容。

範例 5:判斷要更新的控制台檔案

PS C:\> powershell.exe -PSConsoleFile Console01.psc1
PS C:\> Add-PSSnapin MySnapin
PS C:\> Export-Console NewConsole.psc1
PS C:\> $ConsoleFileName
PS C:\> Add-PSSnapin SnapIn03
PS C:\> Export-Console

此範例示範如何使用 $ConsoleFileName 自動變數來判斷如果您使用 Export-Console,而沒有 Path 參數值,將會更新的控制台檔案。

第一個命令會使用 PowerShell.exe 的 PSConsoleFile 參數,使用 Console01.psc1 檔案開啟 Windows PowerShell。

第二個命令會使用 Add-PSSnapin Cmdlet,將 MySnapin Windows PowerShell 嵌入式管理單元新增至目前的會話。

第三個命令會使用 Export-Console Cmdlet,將會話中所有 Windows PowerShell 嵌入式管理單元的名稱匯出至 NewConsole.psc1 檔案。

第四個命令會顯示$ConsoleFileName變數。 它包含最近使用的主控台檔案。 範例輸出顯示 NewConsole.ps1 是最近使用的檔案。

第五個命令會將 SnapIn03 新增至目前的控制台。

第六個命令會使用 Export-Console Cmdlet,而不需要 Path 參數。 此命令會將目前會話中所有 Windows PowerShell 嵌入式管理單元的名稱匯出至最近使用的檔案 NewConsole.psc1。

參數

-Confirm

在執行 Cmdlet 之前,提示您進行確認。

類型:SwitchParameter
別名:cf
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

-Force

表示此 Cmdlet 會覆寫控制台檔案中的數據,而不會發出警告,即使檔案具有只讀屬性也一樣。 只讀屬性已變更,而且當命令完成時不會重設。

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-NoClobber

表示此 Cmdlet 不會覆寫現有的控制台檔案。 根據預設,如果檔案出現在指定的路徑中,Export-Console 會覆寫檔案而不發出警告。

類型:SwitchParameter
別名:NoOverwrite
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Path

指定主控台檔案的路徑和檔名 (*.psc1)。 輸入選擇性路徑和名稱。 不允許通配符。

如果您只指定檔名,Export-Console 會在目前目錄中建立具有該名稱和 .psc1 擴展名的檔案。

除非您已使用 PSConsoleFile 參數開啟 Windows PowerShell,或在目前的會話期間匯出控制台檔案,否則這個參數是必要的。 當您使用 NoClobber 參數以防止覆寫目前的控制台檔案時,也需要它。

如果您省略此參數,Export-Console 會覆寫此工作階段中最近使用的控制台檔案。 最近使用主控台檔案的路徑會儲存在$ConsoleFileName自動變數的值中。 如需詳細資訊,請參閱about_Automatic_Variables。

類型:String
別名:PSPath
Position:0
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-WhatIf

顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。

類型:SwitchParameter
別名:wi
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

輸入

String

您可以使用管線將路徑字串傳送至此 Cmdlet。

輸出

FileInfo

此 Cmdlet 會建立包含導出別名的檔案。

備註

  • 當主控台檔案 (.psc1) 用來啟動工作階段時,控制台檔案的名稱會自動儲存在$ConsoleFileName自動變數中。 當您使用 Export-ConsolePath 參數來指定新的控制台檔案時,會更新 $ConsoleFileName 的值。 未使用主控台檔案時,$ConsoleFileName沒有值($Null)。

    若要在新會話中使用 Windows PowerShell 控制台檔案,請使用下列語法來啟動 Windows PowerShell:

    powershell.exe -PsConsoleFile \<ConsoleFile\>.psc1

    您也可以將 Add-PSSnapin 命令新增至 Windows PowerShell 配置檔,以儲存未來會話的 Windows PowerShell 嵌入式管理單元。 如需詳細資訊,請參閱about_Profiles。