2

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.

Community
  • 1
  • 1
Rynant
  • 23,153
  • 5
  • 57
  • 71
  • I noticed you changed "%~f0" to just %0 presumably for simplification, but you have lost some functionality by doing so. First, the quotes are needed in the event of spaces in the name. Second the more verbose expression %~f0 is needed if you ever run the batch file from the command line and do not explicitly type ".bat" with the name. – Michael Sorens Dec 21 '11 at 22:24

3 Answers3

1

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I've read the help from `powershell /?` many times, but this bit of info didn't stick. I guess I knew I'd never have to use it. . . – Rynant Dec 20 '11 at 14:34
0

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.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Ah, I guess I was too used to `$_` in PowerShell and thought that cmd had something similar. – Rynant Dec 20 '11 at 14:42
-1

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.

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • I know about the options I have for running scripts, and I have my own reasons for wanting to have a single file that a user can double-click to run some PowerShell code. Your answer does not address my question. – Rynant Dec 20 '11 at 14:57
  • @Rynant I'd be interested to hear what kind of use case you have for this sort of thing. I thought I'd try to interpret your intent (wanted to create clickable scripts). Sometimes people ask questions for things because they are not aware of alternatives. I don't know what your use case is maybe you have no alternative. – Andy Arismendi Dec 20 '11 at 22:10
  • I work in a QA department, and everyone isn't familiar with PowerShell. It is convenient to place scripts on the desktops of the lab computers that a user can double-click to perform frequent tasks (open and log into an application, open most recent log file, etc.) Having a single file per script makes it easier to manage. Double-clicking is quicker than right-click and selecting "Run with PowerShell", and I don't want to change .ps1 file associations and have every .ps1 file run when double-clicked. – Rynant Dec 21 '11 at 19:03