Browse All Articles > Powershell create a custom Hello world..! cmdlet using VS2010 and Powershell 2.0
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 View 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
Step 2: Create a new Powershell cmdlet class
Right Click –> Solution Explorer –> Add –> New Item —-> Select Powershell PS cmdlet
Now let’s code for simple hello world in your .cs file
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; } } } }
Comments (1)
Commented:
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