主题
    about_Profiles

简短说明
    描述如何创建和使用 Windows PowerShell 配置文件。

详细说明
    可以创建 Windows PowerShell 配置文件来自定义环境,以及将特定于会话的元素
    添加到您启动的每一个 Windows PowerShell 会话。

    Windows PowerShell 配置文件是在 Windows PowerShell 启动时运行的脚本。可以将配置文件用作登录脚
    本来自定义环境。可以添加命令、别名、函数、变量、管理单元、模块以及 Windows PowerShell 驱动器。
    您还可以在配置文件中添加特定于会话的其他元素,这样这些元素就可以直接在每次会话中存在,而无需
    导入或重新创建它们。

    Windows PowerShell 支持多种针对用户和主机程序的配置文件。但是,它不会为您创建配置文件。
    本主题介绍配置文件,并描述如何在计算机上创建和维护配置文件。

    本文它说明了如何使用 Windows PowerShell 控制台 (PowerShell.exe) 的 NoProfile 参数 
    直接启动 Windows PowerShell(而无需任何配置文件)。此外,本主题还将说明 Windows 
    PowerShell 执行策略对配置文件的影响。


 配置文件

    Windows PowerShell 支持多种配置文件。此外,Windows PowerShell 主机程序可支持其自身
    的特定于主机的配置文件。

    例如,Windows PowerShell 控制台支持以下基本配置文件。这些配置文件按优先级顺序列出。
    第一个配置文件的优先级最高。


        说明			路径
        -----------             ----
        当前用户,当前主机	$Home\[My ]Documents\WindowsPowerShell\Profile.ps1
        当前用户,所有主机	$Home\[My ]Documents\Profile.ps1
        所有用户,当前主机	$PsHome\Microsoft.PowerShell_profile.ps1
        所有用户,所有主机	$PsHome\Profile.ps1

    配置文件路径包含以下变量:

        - $PsHome 变量,它存储 Windows PowerShell 的安装目录。

        - $Home 变量,它存储当前用户的主目录。


    此外,承载 Windows PowerShell 的其他程序可支持其各自的配置文件。例如,
    Windows PowerShell 集成脚本环境 (ISE) 支持以下特定于主机的配置文件。


        说明			路径
        -----------             -----
        当前用户,当前主机	$Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
        所有用户,当前主机	$PsHome\Microsoft.PowerShellISE_profile.ps1


    在 Windows PowerShell 帮助中,"当前用户,当前主机"配置文件一般被称为"您的 Windows 
    PowerShell 配置文件"。

 $PROFILE 变量

    $Profile 自动变量存储当前会话中可用的 Windows PowerShell 配置文件的路径。

    若要查看配置文件路径,应显示 $Profile 变量的值。还可以在命令中使用 $Profile 变量表示路径。

    $Profile 变量存储“当前用户,当前主机”配置文件的路径。其他配置文件
    保存在 $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 变量的当前值,请键入:

        $profile | get-member -type noteproperty

    
    可以在很多命令中使用 $Profile 变量。例如,下面的命令在记事本中
    打开“当前用户,当前主机”配置文件:

	notepad $profile


    下面的命令确定本地计算机上是否创建了“所有用户,所有主机”配置文件:

	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 语句阻止覆盖现有配置文件。
    请将 <profile-path> 占位符的值替换为您要创建的配置文件的路径。

    注:若要在 Windows Vista 及更高版本 Windows 中创建“所有用户”配置文件,
    	请使用“以管理员身份运行”选项启动 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 配置文件中。

    配置文件的另一种常见用法是保存常用函数、别名和变量。将这些项保存在配置文件中之后,
    就可以在任何适用的会话中直接使用这些项,而无需重新创建它们。


 如何开始使用配置文件

    配置文件在打开时是空的。但是,可以在其中填入常用的变量、别名和命令。

    以下是一些入门建议。

    -- 添加使配置文件易于打开的命令。如果使用的是非“当前用户,当前主机”的配置文件,
       那么这一点尤其有用。例如,添加下面的命令:
               
           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 命令,以便添加使用的所有 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-expression 
        "$home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"}

    运行此命令后,配置文件添加到会话的命令可在 $s 中使用。


另请参阅
    about_Automatic_Variables
    about_Functions
    about_Prompts
    about_Execution_Policies
    about_Signing
    about_Remote
    Set-ExecutionPolicy





目录