[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Powershell create a custom Hello world..! cmdlet using VS2010 and Powershell 2.0

Published on
12,053 Points
1 Endorsement
Last Modified:
Writing a custom cmdlet for Powershell can be easier than you might think. Use this step by step guide to help you write your first one and learn the steps. Commands you will use for this task are:

1. Set-alias (Powershell)

2. Installutil (Powershell)

Operations you will perform for this task

1. Create a new Powershell cmdlet project.

2. Create a new Powershell PSCmdlet. (Business logic resides in this class)

3. Create a new Powershell Custom Snap-in (installer for cmdlet)

Step 1: Create a new Powershell cmdlet project

Install Powershell cmdlet templates, download from http://psvs2008.codeplex.com/ these templates even worked on VS2010 for me.

Create a new Powershell cmdlet project, and framework as .NET framework 4
 create a new powershellcmd projectView of your solution explorer after you create a Powershell cmdlet solution, there are 2 new references System.Configuration.Install and System.Management.Automation added to solution
 New added references

Step 2: Create a new Powershell cmdlet class

Right Click –> Solution Explorer –> Add –> New Item —-> Select Powershell PS cmdlet
 Powershell PScmdlet Project

Now let’s code for simple hello world in your .cs file
 WriteObject("Hello world..!") , prints hello world

Step 3: Create a new Powershell Custom Snap-in (installer for cmdlet)

Create a custom snap in for your cmdlet, this is like an installer for Powershell cmdlet and code the .cs file accordingly

Similar to step 2 except this time we are using the template to create the Snap-In and making a few changes (shown below)

Right Click –> Solution Explorer –> Add –> New Item —-> Select Powershell cmdlet Snap in

using System.Collections.ObjectModel;
using System.ComponentModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 

namespace mycustom2 
public class mycustomsnapin : CustomPSSnapIn 
private Collection<CmdletConfigurationEntry> cmdlets = new Collection<CmdletConfigurationEntry>(); 
private Collection<ProviderConfigurationEntry> providers = new Collection<ProviderConfigurationEntry>(); 
private Collection<TypeConfigurationEntry> types = new Collection<TypeConfigurationEntry>(); 
private Collection<FormatConfigurationEntry> formats = new Collection<FormatConfigurationEntry>(); 

public mycustomsnapin() : base() 
cmdlets.Add(new CmdletConfigurationEntry(“get-matches”, typeof(pscommandlet), null)); 

public override string Name 
get { return “mycustomsnapin”; } 

public override string Vendor 
get { return “kn”; } 

public override string Description 
get { return “This snap-in is a test.”; } 

public override Collection<CmdletConfigurationEntry> Cmdlets 
get { return cmdlets; } 

public override Collection<ProviderConfigurationEntry> Providers 
get { return providers; } 

public override Collection<TypeConfigurationEntry> Types 
get { return types; } 

public override Collection<FormatConfigurationEntry> Formats 
get { return formats; } 


Open in new window

Important : Below are the few changes you need to make before you build or deploy
 Change typeof(<yourowncmdletname))

Step 4. deploy your cmdlet in Powershell

Once you create your custom snap in and custom PScmdlet, you need to deploy your .dll. Use the below command to deploy your cmdlet in Powershell.

PS\> PS C:\WINDOWS\system32> Set-Alias installutil C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

PS\> PS C:\WINDOWS\system32>Get-PSSnapin (lists registered snap – ins)
 Get-PSsnapin displays the registered custom snap in

Run custom cmdlet
 Run your custom cmdlet

That’s all , we are done…!.

1 Comment
LVL 16

Expert Comment

by:Dale Harris

Please run this through Microsoft Word for spell check and use consistency when writing Powershell or powershell or PowerShell.

I found two errors at the top:

Not trying to take you down, just trying to help :)


Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Join & Write a Comment

Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month