Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 从cmd运行powershell命令_Windows_Powershell_Cmd - Fatal编程技术网

Windows 从cmd运行powershell命令

Windows 从cmd运行powershell命令,windows,powershell,cmd,Windows,Powershell,Cmd,我是windows环境的新手。如何从cmd执行以下powershell命令“hello gourav你好吗1”| Out File filename-append您可以在cmd中使用powershell.exe执行powershell命令 powershell.exe "your command" 像 您可以直接使用powershell进入powershell中的交互界面powershell可能有点离题,但在没有powershell的情况下也可以使用相同的技巧(即直接从cmd提示符): 回显文

我是windows环境的新手。如何从cmd执行以下powershell命令
“hello gourav你好吗1”| Out File filename-append

您可以在cmd中使用
powershell.exe
执行powershell命令

powershell.exe "your command"


您可以直接使用powershell进入powershell中的交互界面powershell

可能有点离题,但在没有powershell的情况下也可以使用相同的技巧(即直接从cmd提示符):

回显文本>>文件名


从Run和execute powershell/中打开cmd?了解你所有的选择

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

PowerShell[.exe] -Help | -? | /?

-PSConsoleFile
    Loads the specified Windows PowerShell console file. To create a console
    file, use Export-Console in Windows PowerShell.

-Version
    Starts the specified version of Windows PowerShell.
    Enter a version number with the parameter, such as "-version 2.0".

-NoLogo
    Hides the copyright banner at startup.

-NoExit
    Does not exit after running startup commands.

-Sta
    Starts the shell using a single-threaded apartment.
    Single-threaded apartment (STA) is the default.

-Mta
    Start the shell using a multithreaded apartment.

-NoProfile
    Does not load the Windows PowerShell profile.

-NonInteractive
    Does not present an interactive prompt to the user.

-InputFormat
    Describes the format of data sent to Windows PowerShell. Valid values are
    "Text" (text strings) or "XML" (serialized CLIXML format).

-OutputFormat
    Determines how output from Windows PowerShell is formatted. Valid values
    are "Text" (text strings) or "XML" (serialized CLIXML format).

-WindowStyle
    Sets the window style to Normal, Minimized, Maximized or Hidden.

-EncodedCommand
    Accepts a base-64-encoded string version of a command. Use this parameter
    to submit commands to Windows PowerShell that require complex quotation
    marks or curly braces.

-ConfigurationName
    Specifies a configuration endpoint in which Windows PowerShell is run.
    This can be any endpoint registered on the local machine including the
    default Windows PowerShell remoting endpoints or a custom endpoint having
    specific user role capabilities.

-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.

-ExecutionPolicy
    Sets the default execution policy for the current session and saves it
    in the $env:PSExecutionPolicyPreference environment variable.
    This parameter does not change the Windows PowerShell execution policy
    that is set in the registry.

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed
    in braces ({}). You can specify a script block only when running PowerShell.exe
    in Windows PowerShell. The results of the script block are returned to the
    parent shell as deserialized XML objects, not live objects.

    If the value of Command is a string, Command must be the last parameter
    in the command , because any characters typed after the command are
    interpreted as the command arguments.

    To write a string that runs a Windows PowerShell command, use the format:
        "& {<command>}"
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.

-Help, -?, /?
    Shows this message. If you are typing a PowerShell.exe command in Windows
    PowerShell, prepend the command parameters with a hyphen (-), not a forward
    slash (/). You can use either a hyphen or forward slash in Cmd.exe.

EXAMPLES
    PowerShell -PSConsoleFile SqlSnapIn.Psc1
    PowerShell -version 2.0 -NoLogo -InputFormat text -OutputFormat XML
    PowerShell -ConfigurationName AdminRoles
    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
PowerShell[.exe][-PSConsoleFile |-Version]
[-NoLogo][-NoExit][-Sta][-Mta][-NoProfile][-NOINTERACTIVE]
[-InputFormat{Text | XML}][-OutputFormat{Text | XML}]
[-WindowStyle][-EncodedCommand]
[-ConfigurationName]
[-文件][执行策略]
[-Command{-|[-args]
|  [] } ]
PowerShell[.exe]-帮助|-?|/?
-PSConsoleFile
加载指定的Windows PowerShell控制台文件。创建控制台的步骤
文件,请使用Windows PowerShell中的导出控制台。
-版本
启动指定版本的Windows PowerShell。
输入带有参数的版本号,例如“-version 2.0”。
-诺洛戈
在启动时隐藏版权横幅。
-诺西特
在运行启动命令后不退出。
-Sta
使用单线程单元启动shell。
默认为单线程单元(STA)。
-Mta
使用多线程单元启动shell。
-非营利组织
不加载Windows PowerShell配置文件。
-非交互
不会向用户显示交互式提示。
-输入格式
描述发送到Windows PowerShell的数据格式。有效值为
“文本”(文本字符串)或“XML”(序列化的CLIXML格式)。
-输出格式
确定如何格式化Windows PowerShell的输出。有效值
是“文本”(文本字符串)或“XML”(序列化的CLIXML格式)。
-窗式
将窗口样式设置为“普通”、“最小化”、“最大化”或“隐藏”。
-编码命令
接受命令的base-64编码字符串版本。使用此参数
向Windows PowerShell提交需要复杂报价的命令
标记或花括号。
-配置名称
指定运行Windows PowerShell的配置终结点。
这可以是本地计算机上注册的任何端点,包括
默认Windows PowerShell远程处理终结点或具有
特定的用户角色功能。
-文件
在本地范围(“点源”)中运行指定的脚本,以便
脚本创建的函数和变量在
本届会议。输入脚本文件路径和任何参数。
文件必须是命令中的最后一个参数,因为所有字符
在解释文件参数名称后键入
作为脚本文件路径,后跟脚本参数。
-执行政策
设置当前会话的默认执行策略并将其保存
在$env:PSExecutionPolicyPreference环境变量中。
此参数不会更改Windows PowerShell执行策略
这是在注册表中设置的。
-命令
执行指定的命令(和任何参数),就像它们是
在Windows PowerShell命令提示符下键入,然后退出,除非
指定了NoExit。命令的值可以是“-”,一个字符串。或者
脚本块。
如果命令值为“-”,则从标准中读取命令文本
输入
如果Command的值是脚本块,则必须将脚本块括起来
在大括号({})中。只能在运行PowerShell.exe时指定脚本块
在Windows PowerShell中。脚本块的结果将返回给
父shell作为反序列化的XML对象,而不是活动对象。
如果Command的值是字符串,则Command必须是最后一个参数
在命令中,因为在命令之后键入的任何字符都是
解释为命令参数。
要编写运行Windows PowerShell命令的字符串,请使用以下格式:
"& {}"
其中引号表示字符串和调用运算符(&)
导致执行该命令。
-帮助,-?,/?
显示此消息。如果您在Windows中键入PowerShell.exe命令
PowerShell,在命令参数前加上连字符(-),而不是转发
斜杠(/)。可以在Cmd.exe中使用连字符或正斜杠。
例子
PowerShell-PSConsoleFile SqlSnapIn.Psc1
PowerShell-版本2.0-NoLogo-InputFormat文本-OutputFormat XML
PowerShell-配置名称AdminRoles
PowerShell-命令{Get EventLog-LogName security}
PowerShell-命令“&{Get EventLog-LogName security}”
#要使用-EncodedCommand参数,请执行以下操作:
$command='dir“c:\program files”'
$bytes=[System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand=[Convert]::ToBase64String($bytes)
powershell.exe-encodedCommand$encodedCommand
简单

@ECHO off 
SET LONG_COMMAND=^
if ($true)^
{^
$Resolution=Get-DisplayResolution;^
Write-Host $Resolution;^
} 
START Powershell -noexit -command %LONG_COMMAND% 
回报

@echo off
set "psCommand="[Environment]::GetFolderPath('DesktopDirectory')""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
echo DesktopDirectory = %folder%

我投票将此问题作为主题外的问题结束,因为用户没有展示出自己在寻找答案方面的任何努力。使用
powershell-NoProfile-Command“…
将确保任何配置文件输出不会写入文件,并显式指定这是
Command
参数。
@echo off
set "psCommand="[Environment]::GetFolderPath('DesktopDirectory')""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
echo DesktopDirectory = %folder%