about_Profiles

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Profiles

簡短描述

描述如何建立及使用 Windows PowerShell 設定檔。

詳細描述

您可以建立 Windows PowerShell 設定檔來自訂環境,以及將工作階段特定項目加入每個您啟動的 Windows PowerShell 工作階段。

Windows PowerShell 設定檔是在 Windows PowerShell 啟動時執行的指令碼。您可以使用此設定檔做為登入指令碼,以自訂環境。您可以新增命令、別名、函式、變數、嵌入式管理單元、模組和 Windows PowerShell 磁碟機。您也可以加入其他工作階段特定項目到設定檔,讓它們可用於每個工作階段,而不需要匯入或重新建立。

Windows PowerShell 支援數個使用者和主機程式的設定檔。不過,它不會為您建立設定檔。本主題將描述這些設定檔,並且描述如何建立和維護您電腦上的設定檔。

也描述如何使用 Windows PowerShell 主控台的 NoProfile 參數 (PowerShell.exe),在沒有任何設定檔的情況下啟動 Windows PowerShell 。同時也描述設定檔上 Windows PowerShell 執行原則的作用。

設定檔

Windows PowerShell 支援數個設定檔。此外,Windows PowerShell 主機程式可支援自己主機指定的設定檔。

例如,Windows PowerShell 主控台支援下列的基本設定檔。這些設定檔會依照優先順序列出。第一個設定檔具有最高優先順序。

        Description                Path
        -----------                ----
        Current User, Current Host $Home\[My ]Documents\WindowsPowerShell\Profile.ps1
        Current User, All Hosts    $Home\[My ]Documents\Profile.ps1
        All Users, Current Host    $PsHome\Microsoft.PowerShell_profile.ps1
        All Users, All Hosts       $PsHome\Profile.ps1

此設定檔路徑包括下列變數:

- $PsHome 變數將儲存安裝在 Windows PowerShell 的目錄。

- $Home 變數將儲存目前使用者的主目錄。

此外,其他裝載 Windows PowerShell 的程式可支援其自己的設定檔。例如,Windows PowerShell 整合式指令碼環境 (ISE) 支援下列主機指定的設定檔。

        Description                Path
        -----------                -----
        Current user, Current Host $Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
        All users, Current Host    $PsHome\Microsoft.PowerShellISE_profile.ps1

在 Windows PowerShell 說明內,最常將 "CurrentUser, Current Host" 設定檔稱為「您的 Windows PowerShell 設定檔」。

$PROFILE 變數

$Profile 自動變數儲存 Windows PowerShell 設定檔的路徑,可在目前工作階段中提供使用。

若要檢視設定檔路徑,請顯示 $Profile 變數的值。您也可以在命令中使用 $Profile 變數來代表路徑。

$Profile 變數會儲存 "Current User, Current Host" 設定檔的路徑。其他設定檔會儲存在 $profile 變數的 note 屬性。

例如 $Profile 變數會具有下列 Windows PowerShell 主控台的值。

        Name                               Description                
        -----------                        -----------
        $Profile                           Current User,Current Host  
        $Profile.CurrentUserCurrentHost    Current User,Current Host  
        $Profile.CurrentUserAllHosts       Current User,All Hosts     
        $Profile.AllUsersCurrentHost       All Users, Current Host    
        $Profile.AllUsersAllHosts          All Users, All Hosts       
 

因為 $Profile 變數的值會為每個使用者和每個主應用程式而變更,所以請確定在每個您使用的 Windows PowerShell 主應用程式中顯示設定檔變數的值。

若要查看 $Profile 變數目前的值,請輸入:

        $profile | get-member -type noteproperty

您可以在許多命令中使用 $Profile 變數。例如,下列命令會在記事本中開啟 "Current User, Current Host" 設定檔:

notepad $profile 

下列命令會判斷是否已在本機電腦上建立 "All Users, All Hosts" 設定檔:

test-path $profile.AllUsersAllHosts

如何建立設定檔

若要建立 Windows PowerShell 設定檔,請使用下列命令格式:

        if (!(test-path <profile-name>)) 
           {new-item -type file -path <profile-name> -force}

例如,若要在目前的 Windows PowerShell 主應用程式中建立目前使用者的設定檔,請使用下列命令:

        if (!(test-path $profile)) 
           {new-item -type file -path $profile -force}

在這個命令中,此 If 陳述式可避免您覆寫現有的設定檔。請將 <設定檔路徑> 預留位置取代成您想要建立設定檔的路徑。

注意:若要在 Windows Vista 和更新版本的 Windows 上建立 "All Users" 設定檔,請使用 [以系統管理員身分執行] 選項啟動 Windows PowerShell。

如何編輯設定檔

您可以在文字編輯器中開啟任何 Windows PowerShell 設定檔,例如記事本。

若要在記事本中開啟目前 Windows PowerShell 主應用程式之目前使用者的設定檔,請輸入:

        notepad $profile

若要開啟其他設定檔,請指定設定檔名稱。例如,若要開啟所有主應用程式的所有使用者設定檔,請輸入:

        notepad $profile.AllUsersAllHosts

若要套用所做的變更,請儲存此設定檔,然後重新啟動 Windows PowerShell。

如何選擇設定檔

如果您使用多個主應用程式,請將您在所有主應用程式中使用的項目放到 $Profile.CurrentUserAllHosts 設定檔。請將主應用程式特定的項目放入與該主應用程式特定的設定檔中,例如設定主應用程式背景色彩的命令。

如果您是系統管理員,並且替許多使用者自訂 Windows PowerShell,則請遵循下列方針:

-- 在 $profile.AllUsersAllHosts 設定檔中儲存常用的項目。

-- 在主應用程式特定的 $profile.AllUsersCurrentHost 設定檔中儲存該主應用程式特定的項目。

-- 在使用者特定設定檔中儲存特定使用者的項目。

請務必檢查 Windows PowerShell 設定檔之任何特殊實作的主應用程式文件。

如何使用設定檔

許多在 Windows PowerShell 中建立的項目和您所執行的大部分命令,都會影響目前的工作階段。當您結束該工作階段時,便會刪除這些項目。

工作階段特有的命令和項目包含您加入此工作階段的變數、喜好設定變數、別名、函式、命令 (除了 Set-ExecutionPolicy) 和 Windows PowerShell 嵌入式管理單元。

若要儲存這些項目,並讓它們可在後續的所有工作階段中使用,請將它們加入 Windows PowerShell 設定檔。

設定檔的另一個常見用途是儲存常用的函式、別名及變數。當您在設定檔中儲存項目時,您可以在任何適用的工作階段中使用它們,而不需重新建立。

如何啟動設定檔

當您開啟設定檔時,其為空白。不過,您可以將它填滿變數、別名及您經常使用的命令。

以下是一些建議,可讓您開始著手。

-- 加入讓您輕鬆開啟設定檔的命令。如果您使用 "Current User, Current Host" 設定檔以外的設定檔時,這尤其有用。例如,加入下列命令:

           function pro {notepad $profile.CurrentUserAllHosts}

-- 加入會在編譯 HTML 說明檔 (.chm) 中開啟 Windows PowerShell 說明的函式。

           function Get-CHM
            {
               (invoke-item $env:windir\help\mui\0409\WindowsPowerShellHelp.chm)
            }

此函式會開啟該 .chm 檔案的英文版。不過,您可以替換語言代碼 (0409),以開啟該 .chm 檔案的其他版本。

-- 加入會列出任何 Cmdlet 別名的函式。

           function Get-CmdletAlias ($cmdletname)
           {
              get-alias | Where {$_.definition -like "*$cmdletname*"} | ft Definition, Name -auto
           }

-- 加入 Add-PsSnapin 命令來新增任何您使用的 Windows PowerShell 嵌入式管理單元。

-- 自訂您的主控台。

           function Color-Console 
           {
        $host.ui.rawui.backgroundcolor = "white"
        $host.ui.rawui.foregroundcolor = "black"
                $hosttime = (dir $pshome\PowerShell.exe).creationtime
                $Host.UI.RawUI.WindowTitle = "Windows PowerShell $hostversion ($hosttime)"
                clear-host
           }
           Color-console

-- 加入自訂的 Windows PowerShell 提示,其中包含電腦名稱和目前的路徑。

           function prompt 
           {
              $env:computername + "\" + (get-location) + "> "
           }

如需 Windows PowerShell 提示的詳細資訊,請參閱 about_Prompts。

NOPROFILE 參數

若要啟動 Windows PowerShell 而不使用設定檔,則請使用 PowerShell.exe (啟動 Windows PowerShell 的程式) 的 NoProfile 參數。

若要開始,請開啟可以啟動 Windows PowerShell 的程式,例如 Cmd.exe 或 Windows PowerShell 本身。您也可以使用 Windows 中的 [執行] 對話方塊。

類型:

PowerShell -noprofile

如需 PowerShell.exe 參數的完整清單,請輸入:

PowerShell -?

設定檔和執行原則

Windows PowerShell 執行原則在某種程度上會決定您是否可以執行指令碼和載入組態檔,包括設定檔。Restricted 執行原則是預設值。它會阻止所有的指令碼執行,包括設定檔。如果您使用 Restricted 原則,設定檔就不會執行,且不會套用其內容。

Set-ExecutionPolicy 命令可設定和變更執行原則。它是少數可套用在所有 Windows PowerShell 工作階段中的命令,因為該值儲存於登錄中。您不必在開啟主控台時予以設定,也不必在設定檔中儲存 Set-ExecutionPolicy 命令。

設定檔和遠端工作階段

Windows PowerShell 設定檔不會在遠端工作階段自動執行,所以設定檔新增的命令不會出現在遠端工作階段。此外,$profile 自動變數不會在遠端工作階段中填入。

若要在工作階段中執行設定檔,請使用 Invoke-Command Cmdlet。

例如,下列命令會在 $s 的工作階段中執行本機電腦的 CurrentUserCurrentHost 設定檔。

        invoke-command -session $s -filepath $profile

下列命令會在 $s 的工作階段中,執行來自遠端電腦的 CurrentUserCurrentHost 設定檔。由於不會填入 $profile 變數,因此該命令會使用設定檔的明確路徑。

        invoke-command -session $s {invoke-command "$home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"}

執行此命令之後,$s 中將提供設定檔新增到工作階段的命令。

另請參閱

about_Automatic_Variables

about_Functions

about_Prompts

about_Execution_Policies

about_Signing

about_Remote

Set-ExecutionPolicy