Typing a Command on Multiple Lines

This topic contains 4 replies, has 5 voices, and was last updated by  js 1 month ago.

  • Author
    Posts
  • #89572

    Kyle Reese
    Participant

    Hello everyone!

    Trying to learn Powershell, and the lessons I am following tell me that if I hit enter with something like a trailing pipeline character (example: "Get-Process |"), the command prompt will change and I will be allowed to continue typing on the next line. Google searches corroborate this and also tell me I can use the "`" character to achieve the same result.

    Unfortunately, none of this actually happens. If I end the line with a trailing pipeline character, Powershell throws back an error saying "An empty pipe element is not allowed." If I end with a trailing "`", it throws the error "Incomplete string token." There doesn't seem to be any way I can find to accomplish a command spread over multiple lines. What am I missing here?

    Thanks for any help you can provide!

  • #89575

    Don Jones
    Keymaster

    Version? Console? ISE? Other?

  • #89579

    Alex Aymonier
    Participant

    Hi Kyle,

    I am able to do this successfully in the PowerShell console, VSCode and in the ISE when writing a script but not in the ISE console. Is that what you are getting too?

  • #89584

    postanote
    Participant

    The ISE is for all intents and purposes just like the VSC code pane. Code written in the ISE script pane, like VSC code pane is not auto executed when hitting enter, like things are in the ISE console pane. In the ISE and VSC code panes, you have to explicitly select and run code in the panes or save and run the code in the pane.

    The VSC integrated PoSH terminal (if you are using it, it's the DOS cmd prompt by default), is like the PoSH console host, so it will respond the same way.

    The ISE console was 'not' designed to be an integrated console. Unlike the PoSH console host and VSC integrated terminal – PoSH console. You cannot run integrated commands in the ISE console as you can in VSC PoSH console and the PoSH console host.

    The moment you hit enter in the ISE console that means, execute what I just typed before the enter. Hence the errors you are seeing. So, you have to take a different tact with the ISE console host. The ISE console host is for single line commands with all properties, switches, etc. and running pre-built/saved scripts, and as an output window for what you run from the script pane. Note, you could send output to another ISE pane, notepad, etc. as well. In the ISE, there is little reason to write script-like things in the ISE console. That is what the script pane is for.

    Again, the ISE console host is not the PoSH console host and will not act the same way. That was never it's design.
    Like you all, I ran into this in the early days of the ISE, but I learned quickly what to do when and where.

    If you are using one-liners, literal specific one-liners, you can use the ISE script pane or the ISE console pane.
    If you are writing scripts / functions / modules, use the ISE script pane. I would say do the same thing when using VSC.

    Remember there are two PoSH terminals in VSC, depending on how you have it set up. If you set PoSH as you intergrated terminal, it is the same as the PoSH console in operation. However, you can set the terminal to be the PoSH Integrated version (think ISE console host) and it responds the same way as the ISE console host.

    In the VSC with the default startup and PoSH set as the terminal (only 1 PoSH terminal listed in the drop down box), you get this...

    $host

    Name : ConsoleHost
    Version : 5.1.16299.98
    InstanceId : 7d9b1336-aea8-4e98-86fc-c81d462f779f
    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : en-US
    CurrentUICulture : en-US
    PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    DebuggerEnabled : True
    IsRunspacePushed : False
    Runspace : System.Management.Automation.Runspaces.LocalRunspace

    Get-Process |
    >>

    If you set VSC to use the integrated PoSH terminal (2 PoSH terminal listed in the drop down box), you get this.

    PowerShell Integrated Console

    PS C:\Users\postanote> $host

    Name : Visual Studio Code Host
    Version : 1.5.1
    InstanceId : a7d55c08-c508-4f43-b8fd-baa977e9c830
    UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture : en-US
    CurrentUICulture : en-US
    PrivateData :
    DebuggerEnabled : True
    IsRunspacePushed : False
    Runspace : System.Management.Automation.Runspaces.LocalRunspace

    PS C:\Users\postanote> Get-Process |
    An empty pipe element is not allowed.
    At line:0 char:0
    PS C:\Users\postanote>

  • #89630

    js
    Participant

    Trying to pipe from this version of foreach aren't ya?

    PS C:\Users\me> foreach ($i in 1,2,3) { $i } | measure
    
    At line:1 char:30
    + foreach ($i in 1,2,3) { $i } | measure
    +                              ~
    An empty pipe element is not allowed.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : EmptyPipeElement
    

    Here's a workaround:

    PS C:\Users\me> $(foreach ($i in 1,2,3) { $i }) | measure
    
    Count    : 3
    Average  :
    Sum      :
    Maximum  :
    Minimum  :
    Property :
    

    or

    1,2,3 | foreach { $_ } | measure
    

You must be logged in to reply to this topic.