Capitalization rules for identifiers were created to help make types, members, and parameters easier to read. There are several types of casing used, depending on the identifier: camelCasing, PascalCasing, lowercase, and UPPERCASE.

Graham Beer

Graham is an experienced system engineer with a passion for PowerShell and automation. You can follow his blog or reach him on Twitter at @GKBeer.

Writing good code isn’t just about how it performs or its usability. Writing code that is clean and well presented is just as important. I will be looking at how to format and case your code for better readability in .NET.

PascalCasing capitalizes the first character of each word, while camelCasing sets the first letter of the first word in lowercase with each word thereafter starting with a capital letter. Here is an example of both PascalCasing and camelCasing:

Notice, in the examples above, that the keyword for the Class name – Foo – starts with a capital, but the first two letters of the variable name are lowercase, with an uppercase letter for the next word.

Since we are talking about .NET, there is a guide, called the .NET Framework Design Guidelines, which recommends that all identifiers, except parameter names, use PascalCasing. It also has a table which describes the capitalization rules for different types of identifier. Here is a link to the online resource.

We now change our attention slightly to see how these guidelines look in PowerShell. PowerShell, as a language, is not case sensitive, but that doesn’t mean we shouldn’t capitalize our code. Being built on .NET, PowerShell follows the same format. Public identifiers in PowerShell, such as module names, functions, cmdlets, classes, enums, attribute names, and properties, conform to PascalCasing:

Module casing in PowerShell

Module casing in PowerShell

 

Example casing for cmdlets in PowerShell

Example casing for cmdlets in PowerShell

 

Parameter casing in PowerShell

Parameter casing in PowerShell

For two-letter acronyms, PowerShell makes an exception in which both letters are capitalized, such as the automatic variable $PSCmdlet, or cmdlets like New-PSSession and Set-PSBreakpoint.

The language keywords in PowerShell are written in lowercase; for example, foreach, param(), begin, process, and end.

This pattern continues with operators (-eq, -match, -like).

When it comes to naming your variables and modules, camelCase is often applied, but this isn’t a hard-and-fast rule. You could continue with PascalCasing or even use lowercase.

UPPERCASE is generally reserved for keywords in comment-based help, to make them easier to find when looking at code:

These are just a few tips for using different types of casing in your code. Formatting your code by indenting and using these examples of casing will produce beautiful, clean, readable code which will not only benefit others, but yourself as well.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

4+

Users who have LIKED this post:

  • avatar
Share
2 Comments
  1. Ray 1 year ago

    While I capitalize my PowerShell code out of habit from coding in Pascal, Basic and C++, I don't really feel the need to do it for clean code or for anyone else reading my code. As long as the code is formatted properly, a capital Get-Service or a get-service shouldn't matter to anyone reading the code.

    If I'm writing something down and dirty in a one liner or a same script to get something done I'm not going to worry about capitalization. It's about getting the work done.

    1+

    Users who have LIKED this comment:

    • avatar
  2. Luke Leigh 1 year ago

    This is more likely to be relevant for anyone writing modules and is not a die-hard rule of thumb. Although to be honest, if you always follow the formatting rules, it becomes habitual. Most people seem to have a tendency to continue to use Aliases (even in modules) which does make it more difficult to follow when reading to find out exactly what it will do.....I hope most people are doing this rather than running "any old code" downloaded from the internet.

    1+

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account