<

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

Published on
11,952 Points
5,852 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
Comment
Author:Deathrace
1 Comment
 
LVL 16

Expert Comment

by:Dale Harris
Deathrace,

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:
Powerhsell
tempaltes

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

-DH
0

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

Join & Write a Comment

Loops Section Overview
Screencast - Getting to Know the Pipeline

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month