Best Practice: You should avoid the need to use the horizontal scrolling in scripts to keep longs commands more readable.
Explanation:
If you write the following command inside a script, it will be definitely not useful to read.
1 |
Send-MailMessage -From 'sender@domain.com' -To 'recipient01@domain.com' -Bcc 'recipient02@domain.com' -Subject 'Meeting' -Body 'Please find the attached file' -Attachments 'C:\plannings\meeting.xls' -DeliveryNotificationOption OnFailure, OnSuccess -SmtpServer smtp.domain.com -Credential $credentials |
There are several issues :
- If you want to see this command, you need to use the horizontal scrolling
- It is harder to read, update and debug
- If you have an add-on (ex: CIM Explorer), you could need to close that to temporarily increase the width view
Example 1 (with no pipeline)
A better approach is to use splatting:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$parameters = @{ From = 'sender@domain.com' To = 'recipient01@domain.com' Bcc = 'recipient02@domain.com' Subject = 'Meeting' Body = 'Please find the attached file' Attachments = 'C:\plannings\meeting.xls' Dno = 'OnFailure,OnSuccess' Priority = 'High' SmtpServer = 'smtp.domain.com' } Send-MailMessage @parameters |
Example 2 (with pipeline)
1 |
Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version -EA 0 | Where-Object -FilterScript { $_.PSChildName -match '^(?!S)\p{L}' } | Select-Object -Property PSChildName, Version |
And now see the difference here, it is much easier to read.
Note: With ISESteroids, you have the feature “Toggles word wrap”: