Quick tip: break a code, differently.

This tip may be helpful to most of people, but is specifically targeted for people who share code on blogs and any similar media where width is issue at times.

So far I had two solutions, depending on the pipeline “type”:

  • multi-command pipeline – breaking on the pipe symbol
  • single-command pipeline – splatting

I avoid backtick (`) as hell, because single whitespace added by potential user after it and whole code becomes broken.

Today I thought about a way that is sort of abuse of way block comment works. It should be helpful when you do not really want to splat and have single command with lots of long parameters. When I was working on good example (why is it always Send-MailMessage that first pops in my mind when I think about this type of the commands?) I got another way – herestring. Both let you break lines almost after each parameter, but herestring is useful probably only for parameters similar to –Body: very long that may brake between several lines.

How such code looks like?

Send-MailMessage -From Bartek.Bielawski@live.com <#
#> -To Mr.Bean@microsoft.com <#
#> -Subject 'This is is just a test message' <#
#> -Body @"
This is message body.
Luckily, this is here-string
so no extra-work to fix it!
"@ -SmtpServer that.wont.work.com

Now you can simply continue typing, without using splatting or risking issues with backtick at the other end of the wire.


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 )

Facebook photo

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

Connecting to %s