<

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

Published on
12,167 Points
6,067 Views
1 Endorsement
Last Modified:
Approved
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 
{ 
[RunInstaller(true)] 
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
Author:Deathrace
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
COLLABORATE WITH CERTIFIED PROFESSIONALS
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.