Does this site look plain?

This site uses advanced css techniques

In a previous cmdletter I recommended creating a base class for all your cmdlets to hold common processing, and I have been using this more and more.

Error reporting/handling is a common use case, and one part of this can be reporting the name of the cmdlet throwing the error. For a time I simply passed a string to whereever needed this (perhaps to a WriteVerbose(), but finally tripped across the simple built-in way to do this, which I've put inside the base class:

public abstract class MyCmdlet : PSCmdlet {

    protected string CmdletName => this.MyInvocation.InvocationName;

    // more helper code later

Now, some other helper code in the same base class, called by multiple cmdlets, can simply refer to CmdletName and reliably report which cmdlet in a pipeline is providing the message.

Tip: in spite of the example-y name, "MyInvocation" is the Microsoft-provided name for this property of the PSCmdlet class, not one created for this page.

The CmdletName property is not available from the base Cmdlet class, only PSCmdlet.

First published: 2019/07/05

The excellent icons on this page were made by Freepik from, and are licensed by CC 3.0 BY