#PowershellBasics: Multi-line commands

2

July 19, 2022 by Kenneth Fisher

When I’m sharing code one of the things I hate most is having a scroll bar along the bottom. I want ya’ll to be able to see all of the code at once. Or at most have to do a bit of scrolling up and down. In my last couple of posts I had some lines of code that were a bit longer and scroll bar started annoying me enough that I decided to figure out how to fix it. You know, it’s kind of funny how much I learn through being annoyed at something enough that I decide to fix it. Anyway, this would be easy enough for me to do in T-SQL. But Powershell? I had no clue.

This is what it looked like when I first wrote this particular line of code.

Get-Service | Where-Object {$_.Status -eq "Running"} | Select-Object Status, Name, DisplayName | Export-Csv -Path C:\temp\Services.csv -Delimiter '|'

So I spent some time and looked up how (it didn’t really take that long, but I did have to look it up.)

Get-Service | Where-Object {$_.Status -eq "Running"} | `
    Select-Object Status, Name, DisplayName | `
    Export-Csv -Path C:\temp\Services.csv -Delimiter '|'

That’s what it looks like now. Much better right? Nice and easy to read. And all it takes is the ` character at the end of the line that you are wrapping. To me it looks like a spec of dirt on my screen but basically it’s a single quote leaning from left to right. Turns out it’s even called a backtick.

I’ll be honest, this is one of those posts where the thought “This is way to simple, it’s not worth posting” went through my head. That said, I did have to look the information up, and I’m absolutely certain I’m not the only one. So there you go. One more place the information can be found.

2 thoughts on “#PowershellBasics: Multi-line commands

  1. Will Lamers says:

    Why not simply break at the Pipes? Seems to work fine for me…

    • Sorry for taking so long to respond to this! And yes, you are right, you can break on the pipes. But it’s still nice to know how to break in other spots. Even a single command (no pipes) can get pretty long at times.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,753 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: