Tips and Tricks

Getting complex – More line breaks in Powershell

Tim Curwick
2 min read
Share:

This is a follow up to Jacob Moran’s article Keeping it simple - Line breaks in PowerShell.
I am strongly in the pro backtick camp, but I won’t get into that debate here. Instead, I’ll cover more of the common ground between the two camps.
In addition to after a pipe, there are many, many more places where you can put in a line break without a backtick and without breaking your code.
As a rule of thumb, any spot where the syntax unambiguously must be followed by something more, you can break the line.
As an extreme example, this:

$A

=

1

,

1

1

,

3

$B

=  @(  “a”

,

“b”

,

“c”  ) If (  $A

[

2

]

. ToString()  -eq

$B

[

2

]

. Length  -or  ( Get-Date ) . Date . DayOfWeek  -eq

‘Tuesday’  ) {  [pscustomobject] @{ Name 

“x”  } }

Can be written like this:

$A

=

1

,

1

1

,

3

$B

=  @(

“a”

“b”

“c”

)

If  (

$A

[

2

]

.

ToString( )  -eq

$B

[

2

]

.

Length  -or

(

Get-Date

) .

Date .

DayOfWeek  -eq

‘Tuesday’

) {

[ pscustomobject] @{ Name 

“x”

} }

That example is, of course, silly.
But combine judicious use of the line break with appropriate horizontal whitespace, and you can turn this:

If  (  $SourceFile1

. Length  /

1Gb

-gt

$MaxSizeGB

-and  (  $SourceFile1

. FullName  -like

“*\Accounting*”

-or

$SourceFile1

. FullName  -like

“*\Finance*”  ) ) {

$Destination

=

$SourceFile1

. FullName . Replace(  $SourceShare

,

$DestinationShare  ) . Replace(  ‘\Accounting'

,

‘\ACC'  ) . Replace(  ‘\Accounting'

,

‘\FIN'  ) }

Into this:

If  (  $SourceFile1

. Length  /

1Gb

-gt

$MaxSizeGB

-and

     ( 

$SourceFile1

. FullName  -like

“*\Accounting*”

-or

       $SourceFile1

. FullName  -like

“*\Finance*”  ) )

    {

    $Destination

=

$SourceFile1

. FullName .

                    Replace( 

$SourceShare

,

$DestinationShare  ) .

                    Replace( 

‘\Accounting'

,

‘\ACC'  ) .

                    Replace( 

‘\Accounting'

,

‘\FIN'  )

    }

Related Articles

Jul 8, 2021

So you want to start a User Group

But where do you begin? I’ve blogged about this from the reversed perspective on my own blog about finding user groups with a small section about what you can do if your thinking about getting one off the ground which you can read at http://blog.kilasuit.org/2016/04/17/how-to-find-local-user-groups-events-my-experience/ and it was only natural to eventually blog from the other side too although this has come up a bit earlier than I had planned to but alas it gets it done As the Coordinator for the UK PowerShell User Groups I learned a few things the hard way with setting up a user group and here are just a few things that you will need to get sorted first which will hopefully help you on your way.

Dec 16, 2020

Media Sync: Organize Your Photos and Videos with PowerShell

Do you have photos and videos that you have taken over the years that are scattered all over the place? Do you want to have all your photos and videos organized? Do you want all your photos and videos to have a standardized naming scheme? If you answered YES to these questions, then this is the post for you. In this post, I will provide you with the PowerShell code and examples for how to use the Media Sync script.