What is the dash (“-”) when used with pipe (“|”) i

2019-07-16 15:35发布

I wanted to create some clickable PowerShell scripts, and I found this answer that I modified slightly to be:

;@Findstr -bv ;@F %0 | powershell -noprofile -command - & goto:eof

# PowerShell Code goes here.

I understand Findstr is passing all lines that don't begin with ;@F to the right-hand side of the pipe and the dash specifies where the input should go, but what is the dash character called and where is it documented?

I found an explanation of CMD's pipe operator on Microsoft's Using command redirection operators, but it doesn't mention anything about the dash character.

3条回答
\"骚年 ilove
2楼-- · 2019-07-16 16:20

I presume you mean the - that precedes the &. It has nothing to do with the pipe operator, it is a directive for powershell.

Here is a description of the -Command option excerpted from powershell help (accessed by powershell /?)

-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.

BTW - I did not realize FINDSTR accepted - as an option indicator until I saw your question. I've only seen and used /. Good info to know.

查看更多
戒情不戒烟
3楼-- · 2019-07-16 16:21

The - is to Powershell saying accept the command(s) from stdin rather than from arguments. This is not a feature in cmd / batch and piping. It would work with < as well.

查看更多
叛逆
4楼-- · 2019-07-16 16:41

Powershell version 2 adds a "Run with Powershell" right-click context menu item to run scripts . Here you'll find some enhanced shell extensions to run Powershell scripts with elevated privileges. However if you just want to run a Powershell script by double clicking a file, I recommend just calling the Powershell script from a batch script instead of trying to embed Powershell code in the batch script. In the batch script use this: powershell.exe -file "%~dp0MyScript.ps1" where %~dp0 expands to the current directory. This essentially creates a bootstrapper for your Powershell script that you can double click to launch your Powershell script.

查看更多
登录 后发表回答