Click to Rate and Give Feedback
MSDN
MSDN Library

  Switch on low bandwidth view
How to Create a Windows PowerShell Cmdlet

This section describes how to create cmdlets to run in the Windows PowerShell™. A cmdlet is a command implemented by deriving a class from one of two specialized Windows PowerShell base classes. The derived class must define public properties that use attributes to identify them as cmdlet parameters and overrides one or more of the input processing methods to process records. The finished cmdlet can be registered with Windows PowerShell, which provides both programmatic and command-line access to the functionality of the cmdlet.

Cmdlet Base Classes

Windows PowerShell supports cmdlets that are derived from two different base classes:

  • Most cmdlets are based on .NET classes that derive from the Cmdlet base class. Deriving from this class means that your cmdlet is using the minimum set of dependencies on the Windows PowerShell runtime. This has two benefits. First, your cmdlet objects are smaller and you are less likely to be impacted by changes to the Windows PowerShell runtime. Second, if you have to, you can directly create an instance of the cmdlet object and invoke it from your cmdlet instead of invoking it from the command line.

  • More complex cmdlets are based on .NET classes that derive from the PSCmdlet base class. Deriving from this class gives you much more access to the Windows PowerShell runtime. This access allows your cmdlet to call scripts, access providers, and access the current session state (getting and setting session variables and preferences). However, it also increases the size of the cmdlet object, and it means that your cmdlet is more tightly coupled to the current version of the Windows PowerShell runtime.

In general, unless you need the extended access to the Windows PowerShell runtime, you should derive from the Cmdlet class. However, the Windows PowerShell runtime has extensive logging capabilities for the execution of cmdlets. If your auditing model depends on this logging, then you want to prevent the execution of your cmdlet from within another cmdlet by deriving from the PSCmdlet class

Location of Reference Assemblies

Be aware that when writing cmdlets, the Windows PowerShell™ reference assemblies are downloaded onto disk (by default at C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0).They are not installed in the Global Assembly Cache (GAC).

Creating Cmdlet

 

Topic Definition

Creating a Cmdlet without Parameters

This section describes how to create a cmdlet that derives from the Cmdlet class.

This first cmdlet is a Get-Proc cmdlet that retrieves information from the local computer without the use of parameters, and then displays the information to the user.

Adding Parameters that Process Command-Line Input

This section describes how to add a parameter that processes command-line input to your cmdlet.

The cmdlet built in this section is an extension of the Get-Proc cmdlet created in Creating a Cmdlet without Parameters.

Adding Parameters that Process Pipeline Input

This section describes how to add a parameter that processes pipeline input to your cmdlet.

The cmdlet built in this section is an extension of the Get-Proc cmdlet created in Creating a Cmdlet without Parameters.

Adding Non-Terminating Error Reporting to Your Cmdlet

This section describes how to add error reporting to your cmdlet.

Creating a Cmdlet that Modifies the System

This section describes how to create a cmdlet that supports system modifications.

This section introduces the Stop-Proc cmdlet that attempts to stop processes running on the local computer.

Adding User Messages to Your Cmdlet

This section describes how to add the ability to write user messages, debug messages, warning messages, and progress information to your cmdlet.

The cmdlet built in this section is an extension of the Stop-Proc cmdlet created in Creating a Cmdlet that Modifies the System.

Adding Aliases, Wildcard Expansion, and Help to Cmdlet Parameters

This section describes how to create a cmdlet that supports parameter aliases, Help, and wildcard expansion.

The cmdlet built in this section is an extension of the Stop-Proc cmdlet created in Creating a Cmdlet that Modifies the System.

Adding Parameter Sets to a Cmdlet

This section describes how to add parameter sets to your cmdlet. Parameter sets allow the cmdlet to operate differently based on what parameters are specified by the user.

The cmdlet built in this section is similar to the Stop-Proc cmdlet created in Creating a Cmdlet that Modifies the System, however, this cmdlet executes within a runspace so it must derive from the PSCmdlet class.

Creating a Cmdlet to Access a Data Store

This section describes how to create a cmdlet that accesses stored data using a Windows PowerShell provider.

This section introduces the Select-Str cmdlet that locates and selects strings that are in a file or object.

Registering the Cmdlet

After implementing a cmdlet, you must register it with Windows PowerShell through a Windows Powershell snap-in. For more information about registering cmdlets, see How to Register Cmdlets, Providers, and Host Applications.

See Also



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker