Word-wrap PowerShell commands

PowerShell commands tend to have a lot of parameters and values you have to give with them. This makes the code you have to write on the command-line or in your scripts very long and it is wider than your screen. So, you cannot see the whole command without scrolling right and left.

This is annoying and time consuming.

You can break long command with a escape character and write them on several lines. I am looking for the correct name of the character, it should be a French "accent grave". I hope this is the correct name in English, too.

On a keyboard with German layout you get it with this key combination:

Keyboard with German layout, Shift key and key with "accent aigu" and "accent grave" are marked. With these two keys pressed together you get an "accent grave".

With this you can cut long commands into several shorter parts and write them on multiple lines. Please remember that you have to go to a new line with "Shift+Enter" on the PS command-line, otherwise the partial command is executed and will fail....

One other solution is to wrap a command after a pipe (|) symbol.

Example:

// Some random code snippet I have grabbed from one of my scripts with out wrapping
$sessions = get-vbrbackupsession | where { ($_.CreationTime -ge $reportstartdate -and $_.CreationTime -le $reportstopdate) -and (($_.JobType -eq 'Backup') -or ($_.JobType -eq 'NasBackup')) } | Sort-Object -property Name, CreationTime

// And the same with wrapping...
$sessions = get-vbrbackupsession | 
   where { ($_.CreationTime -ge $reportstart -and $_.CreationTime -le $reportend) `
   -and (($_.JobType -eq 'Backup') `
   -or ($_.JobType -eq 'NasBackup')) } |
   Sort-Object -property Name, CreationTime

Last updated