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 Smile

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. The Media Sync script utilizes the Shell.Application COM object to gather file metadata. Only files that have a picture or video metadata type will be processed. The script uses the date taken for pictures and the media created metadata fields to organize the photos and videos. If there is no date taken or media created available for a given file, the script will use the modify date instead. The script also ensures that you won’t have any duplicate files by checking the file hashes of the two files in question. If the script detects duplicate files, it will only keep one copy of the file. There are also tools included to help you cleanup unwanted files or folders, delete empty directories and find duplicate files. The script has a simple menu driven PowerShell GUI similar to what I did in a previous post . The Media Sync PowerShell script provides the following features: