Capitalization and Scripts

Over time I’ve learned to write my scripts better. I try to follow best practices, at least as far as I can allow myself. One such best practice is capitalization and before we get to far down this rabbit hole, remember that the suggestions below are not required for your script to run and can turn into an OCD issue for others. Let’ dive in.

Why Capitalization

Capitalization makes sentences more readable (and proper format) and in PowerShell capitalization performs the same function (without the requirement). Readability

Let’s look at some examples:

Cmdlets (w/o capitalization)
[sourcecode language=”powershell”]
get-complianceretentioneventtype
get-devicecompliancedetailsreportfilter
new-azurermapplicationgatewaywebapplicationfirewallconfiguration
[/sourcecode]

Cmdlets (w/ capitalization
[sourcecode language=”powershell”]
Get-ComplianceRetentionEventType
Get-DeviceComplianceDetailsReportFilter
New-AzureRmApplicationGatewayWebApplicationFirewallConfiguration
[/sourcecode]
Code sections (w/o capitalization)
[sourcecode language=”powershell”]
$server = $env:computername
$exchangeproductversion = (gcm exsetup |%{$_.fileversioninfo}).productversion
if ($exchangeproductversion -like '15.02.*') {$version = '2019'}
if ($exchangeproductversion -like '15.01.*') {$version = '2016'}
if ($exchangeproductversion -like '15.00.*') {$version = '2013'}
if ($exchangeproductversion -lt '15') {$exit = $true}
$serverrole = (get-exchangeserver $server).serverrole
$exinstall = (get-itemproperty hklm:\software\microsoft\exchangeserver\v15\setup).msiinstallpath
}
[/sourcecode]
Code sections (w/ capitalization)
[sourcecode language=”powershell”]
$Server = $env:COMPUTERNAME
$ExchangeProductVersion = (GCM exsetup |%{$_.Fileversioninfo}).ProductVersion
If ($ExchangeProductVersion -like '15.02.*') {$Version = '2019'}
If ($ExchangeProductVersion -like '15.01.*') {$Version = '2016'}
If ($ExchangeProductVersion -like '15.00.*') {$Version = '2013'}
If ($ExchangeProductVersion -lt '15') {$Exit = $True}
$ServerRole = (Get-ExchangeServer $Server).ServerRole
$ExInstall = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup).MsiInstallPath
}
[/sourcecode]
Notice that capitalization makes it easier to interpret the cmdlet name of function code. This speeds up your work with the script (building, troubleshooting and correcting) and/or helps the next person understand what they script was meant for easier as well. Those long PowerShell cmdlets are profoundly easier to read with correct word capitalization.

How Far to Take Capitalization

Since capitalization technically isn’t required and is considered a best practice only, your use of capitalization should match your needs for the script. It certainly won’t make the script run faster or better or any different from a script where capitalization is used infrequently. As we see from the examples above, it improves the readability of PowerShell code and can assist in assessing what a script does (visual cues). Do you have to capitalize the first letter of everything in the script? No, if we were to miss some of the first letters, or just plain forgot to capitalize things, it will just affect the visual cues of the script.

That being said, practice. As you’re typing, capitalize your words. Think about it consciously. After a line is done, look at it and see if you words are capitalized or if there are things that don’t look quite right. Fix them then and there so you do not have to go back to the script or code section again.

Practice. Again. It will become second nature eventually. Trust me, it took a long time to change my frame of mind on this … and I still slip back to the old method if I am writing a throwaway script.

Related Post

CIM to WMICIM to WMI

Years ago WMI was de-emphasized for CIM. Wait. Do you know what those are? CIM – “The Common Information Model (CIM) is an extensible, object-oriented data model that contains information