Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PowerShell backtick character for snippet execution doesn't work

When I run the backtick (`) command on this snippet:

Get-WmiObject win32_service | Where-Object { $_.pathname -notlike "C:\windows\*" -and $_.startmode -eq "auto" -and $_.startname -eq "localsystem"} | Select-Object displayname, `
pathname, startmode, startname | Format-List | Out-Host

I get some errors. What are those errors?

First, by pressing F8 just on the first line, I get this:

Incomplete string token. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : IncompleteString

Objective: Running the snippet and assuming the PC will jump onto the next line automatically, since it has the ` character.

Second, when I highlight just the first line, by clicking to the left of the line numbers, I now get this:

At line:1 char:170

  • ... to" -and $_.startname -eq "localsystem"}|Select-Object displayname, `
  •                                                                    ~ Missing expression after ',' > in pipeline element.
    
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : MissingExpression

Objective: Running the snippet and assuming the PC will jump onto the next line automatically, since it has the ` character.

However, when I press F5 it works like a charm. Please forgive my ignorance on PowerShell, but what am I doing wrong here?

Additional info: This is my powershell information:

  • Name: Windows PowerShell ISE Host
  • Version: 5.1
like image 845
Birdie Avatar asked Oct 21 '25 23:10

Birdie


1 Answers

Your errors are coming from features of PowerShell ISE.

First you're trying to use F8 which is "Run Selection", on the first line, with no selection made. That will implicitly select all the characters of the first line, then try to run that.

When you do so, you get the incomplete string token error, and that's because the parser has encountered the lone backtick ` (the escape character), with no character following it. That's because the newline at the end of the line, which the backtick is usually escaping (that's how it works as a line continuation character), is missing, since the single line selection didn't include it.

Second, when I highlight just the first line, by clicking to the left of the line numbers

Now in this case you'll notice that your selection has placed the cursor at the beginning of the next line. That means this selection does include the newline, and so you now have a complete string token.

Your error is that you've now ended your command with a comma, as though you're going to pass more parameters, and then nothing comes after (the rest of the parameters were not included in your selection).


The root of the issue is that the commands in ISE that deal with running a selection, are doing exactly that, so if you want them to include things that are on the next line, you must include them in the selection too.


As a side note, I might recommend that you look for code elements which let you naturally use line breaks, such as the pipe | character, operators, and scriptblock braces {}.

Get-WmiObject win32_service | 
    Where-Object { 
        $_.pathname -notlike "C:\windows\*" -and 
        $_.startmode -eq "auto" -and 
        $_.startname -eq "localsystem"
    } | 
    Select-Object displayname, pathname, startmode, startname | 
    Format-List | 
    Out-Host

This doesn't solve your selection problem, but it's nicer to read in my opinion, and doesn't require the awkward backtick.

like image 190
briantist Avatar answered Oct 25 '25 00:10

briantist