about_PowerShell.exe

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0

主题

about_PowerShell.exe

简短说明

说明如何使用 PowerShell.exe 命令行工具。展示语法并介绍命令行开关。

PowerShell.exe 启动一个 Windows PowerShell® 会话。你可以在 Cmd.exe 和 Windows PowerShell 中使用它。

详细说明

语法

     PowerShell[.exe]
       [-EncodedCommand <Base64EncodedCommand>]
       [-ExecutionPolicy <ExecutionPolicy>]
       [-InputFormat {Text | XML}] 
       [-Mta]
       [-NoExit]
       [-NoLogo]
       [-NonInteractive] 
       [-NoProfile] 
       [-OutputFormat {Text | XML}] 
       [-PSConsoleFile <FilePath> | -Version <Windows PowerShell version>]
       [-Sta]
       [-WindowStyle <style>]
       [-File <FilePath> [<Args>]]
       [-Command { - | <script-block> [-args <arg-array>]
                     | <string> [<CommandParameters>] } ]
     PowerShell[.exe] -Help | -? | /?

参数

-EncodedCommand <Base64EncodedCommand>

接受 Base 64 编码字符串版本的命令。使用此参数向需要复杂引号或大括号的 Windows PowerShell 提交命令。

-ExecutionPolicy <ExecutionPolicy>

设置当前会话的默认执行策略,并将其保存在 $env:PSExecutionPolicyPreference 环境变量中。此参数不更改在注册表中设置的 Windows PowerShell 执行策略。有关 Windows PowerShell 执行策略的信息(包括有效值的列表),请参阅 about_Execution_Policies (https://go.microsoft.com/fwlink/?LinkID=135170)。

-File <FilePath> [<Parameters>]

在本地作用域 ("dot-sourced") 中运行指定脚本,以便脚本创建的函数和变量在当前会话中可用。输入脚本文件路径和任何参数。文件必须为命令中的最后一个参数,因为在 File 参数名称之后键入的所有字符都被解释为后跟脚本参数的脚本文件路径。

你可以在 File 参数的值中包括脚本的参数和参数值。例如:

-File .\Get-Script.ps1 -Domain Central

通常,包括或省略脚本的开关参数。例如,以下命令使用 Get-Script.ps1 脚本文件的 All 参数:

-File .\Get-Script.ps1 -All

在极少数的情况下,你可能需要为开关参数提供一个布尔值。若要在 File 参数的值中为开关参数提供布尔值,则将参数名称和值括在大括号中,如下所示:

-File .\Get-Script.ps1 {-All:$False}.

-InputFormat {Text | XML}

介绍发送到 Windows PowerShell 的数据的格式。有效值为 "Text"(文本字符串)或 "XML"(序列化的 CLIXML 格式)。

-Mta

使用多线程单元启动 Windows PowerShell。此参数是在 Windows PowerShell 3.0 中引入的。在 Windows PowerShell 2.0 中,多线程单元 (MTA) 为默认值。在 Windows PowerShell 3.0 中,单线程单元 (STA) 为默认值。

-NoExit

在运行启动命令后不退出。

-NoLogo

在启动时隐藏版权横幅。

-NonInteractive

不向用户显示交互式提示。

-NoProfile

不加载 Windows PowerShell 配置文件。

-OutputFormat {Text | XML}

确定如何对来自 Windows PowerShell 的输出进行格式设置。有效值为 "Text"(文本字符串)或 "XML"(序列化的 CLIXML 格式)。

-PSConsoleFile <FilePath>

加载指定的 Windows PowerShell 控制台文件。请输入控制台文件的路径和名称。若要创建控制台文件,请在 Windows PowerShell 中使用 Export-Console cmdlet。

-Sta

使用单线程单元启动 Windows PowerShell。在 Windows PowerShell 2.0 中,多线程单元 (MTA) 为默认值。在 Windows PowerShell 3.0 中,单线程单元 (STA) 为默认值。

-Version <Windows PowerShell Version>

启动指定版本的 Windows PowerShell。有效值为 2.0 和 3.0。必须在系统上安装你指定的版本。如果计算机上安装了 Windows PowerShell 3.0,则 "3.0" 为默认版本。否则,"2.0" 为默认版本。有关详细信息,请参阅 Windows PowerShell 入门指南中的“安装 Windows PowerShell”。

-WindowStyle <Window style>

设置会话的窗口样式。有效值包括 Normal、Minimized、Maximized 和 Hidden。

-Command -param

执行指定的命令(以及任何参数),如同在 Windows PowerShell 命令提示符处键入了它们一样,然后退出,除非指定了 NoExit 参数。

Command 的值可以为 "-"、字符串或脚本块。如果 Command 的值为 "-",则从标准输入读取命令文本。

必须将脚本块括在大括号 ({}) 中。仅当在 Windows PowerShell 中运行 PowerShell.exe 时才能指定脚本块。脚本的结果被作为反序列化的 XML 对象(而不是活动对象)被返回到父 shell。

如果 Command 的值为字符串,则 Command 必须为命令中的最后一个参数,因为在命令之后键入的任何字符都将被解释为命令参数。

若要编写运行 Windows PowerShell 命令的字符串,请使用此格式:

         "& {<command>}"

在引号指示字符串以及调用运算符 (&) 导致命令被执行的地方。

-Help, -?, /?

显示关于 PowerShell.exe 的帮助。如果你在 Windows PowerShell 中键入 PowerShell.exe 命令,则在命令参数前面添加连字符 (-),而不是正斜杠 (/)。你可以在 Cmd.exe 中使用连字符或正斜杠。

备注

故障排除注释:在 Windows PowerShell 2.0 中,从 Windows PowerShell 控制台启动某些程序失败,LastExitCode 为 0xc0000142。

示例

    PowerShell -PSConsoleFile sqlsnapin.psc1

    PowerShell -Version 2.0 -NoLogo -InputFormat text -OutputFormat XML

    PowerShell -Command {Get-EventLog -LogName security}

    PowerShell -Command "& {Get-EventLog -LogName security}"

# To use the -EncodedCommand parameter:
    $command = "dir 'c:\program files' "
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    powershell.exe -encodedCommand $encodedCommand

另请参阅

about_PowerShell_Ise.exe (https://go.microsoft.com/fwlink/?LinkID=256512)