The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to Write a Simple Cmdlet


This topic shows how to write a simple cmdlet (the Send-Greeting cmdlet) that takes a single user name as input and then writes a greeting to that user. Although the cmdlet does not do much work, this example demonstrates the major sections of a cmdlet.

To Write a Simple Cmdlet

  1. Use the Cmdlet attribute to declare the class as a cmdlet. This attribute specifies the verb and the noun for the cmdlet name.

    For more information about how to declare the Cmdlet attribute, see CmdletAttribute Declaration.

  2. Specify the name of the class.

  3. Specify that the cmdlet derives from the Cmdlet or PSCmdlet class.

  4. Use the Parameter attribute to define the parameters for the cmdlet. In this case, only one required parameter is specified.

    For more information about how to declare the Parameter attribute, see ParameterAttribute Declaration.

  5. Override the input processing method that will process the input. In this case, the ProcessRecord method is overridden.

  6. Use the Overload:System.Management.Automation.Cmdlet.WriteObject method to write the following greeting: "Hello [UserName]!".


using System.Management.Automation;  // Windows PowerShell assembly.

namespace SendGreeting
  // Declare the class as a cmdlet and specify and 
  // appropriate verb and noun for the cmdlet name.
  [Cmdlet(VerbsCommunications.Send, "Greeting")]
  public class SendGreetingCommand : Cmdlet
    // Declare the parameters for the cmdlet.
    public string Name
      get { return name; }
      set { name = value; }
    private string name;

    // Overide the ProcessRecord method to process
    // the supplied user name and write out a 
    // greeting to the user by calling the WriteObject
    // method.
    protected override void ProcessRecord()
      WriteObject("Hello " + name + "!");