Pablo Galiano : Creating a PowerShell cmdlet

Subscriptions

<March 2010>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News

Subscribe to Pablo Galiano by Email

Post Categories

Creating a PowerShell cmdlet

Some time ago kzu told me about a shell extension called CleanSources, the purpose of this application is to recursive delete all the files and folders under the \bin and \obj directories for a given path.

This tool is very usefull if we dont want to open a Visual Studio and do a Clean Solution/Project for every source code that we want to clean.

So, based on this idea I will create a PowerShell cmdlet to replicate that functionality.

 

These are the detailed steps:

Create a Visual Studio project:

Create a class library project and add a reference to System.Managment.Automation.dll located at %programfiles%/Windows PowerShell\v1.0 and to System.Configuration.Install.dll

 

Create the cmdlet class:

    [Cmdlet(VerbsCommon.Remove, "Binaries")]

    public class CleanerCmdLet : PSCmdlet

    {

    }

 

Add the cmdlet parameter:

        private string basePath;

 

        [Parameter(Mandatory = true, Position = 0)]

        public string BasePath

        {

            get { return basePath; }

            set { basePath = value; }

        }

 

Implement the cmdlet functionality:

        protected override void ProcessRecord()

        {

            CleanSubDirectory(this.basePath);

        }

 

        private void CleanSubDirectory(string path)

        {

            foreach(string subDirectory in Directory.GetDirectories(path))

            {

                DirectoryInfo info = new DirectoryInfo(subDirectory);

 

                if(info.Name.ToLower().Equals("bin") || info.Name.ToLower().Equals("obj"))

                {

                    try

                    {

                        info.Delete(true);

                    }

                    catch(UnauthorizedAccessException)

                    {

                        //Do nothing if the file is readonly

                    }

                }

                else

                {

                    CleanSubDirectory(subDirectory);

                }

            }

        }

 

Create the snap-in class:

using System;

using System.Collections.Generic;

using System.Text;

using System.Management.Automation;

using System.Management.Automation.Runspaces;

using System.Collections.ObjectModel;

using System.ComponentModel;

 

    [RunInstaller(true)]

    public class RemoveBinariesPSSnapIn : 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 RemoveBinariesPSSnapIn()

            : base()

        {

            cmdlets.Add(new CmdletConfigurationEntry("remove-binaries", typeof(CleanerCmdLet), null));

        }

 

        public override string Name

        {

            get { return "RemoveBinariesPSSnapIn"; }

        }

 

        public override string Vendor

        {

            get { return "Clarius Consulting"; }

        }

 

        public override string Description

        {

            get { return "This snap-in contains the Remove Binaries cmdlet."; }

        }

 

        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; }

        }

 

    }

 

Deploy it:

Compile the class library project and open a Visual Studio command prompt (using console :)) and run installutil.exe over the compiled assembly

 

Test it:

Open a PowerShell session

Execute the Get-PSSnapin -Registered cmdlet, if the registration process was correct this should be the output of that cmdlet:

PS C:\> Get-PSSnapin -Registered

Name        : RemoveBinariesPSSnapIn
PSVersion   : 1.0
Description : This snap-in contains the Remove Binaries cmdlet.

This means that cmdlet is registered, but we need to add it to our shell configuration:

PS C:\> Add-PSSnapin RemoveBinariesPSSnapIn

The last step is to execute it:

Remove-Binaries C:\Test

 

Enjoy.

 

Pablo

posted on Tuesday, August 08, 2006 6:06 PM by pga

# PowerShell: how to unit test your cmdlet @ Thursday, October 26, 2006 9:04 AM

If you miss VS, intellisense, TD.NET, etc., you might want to try extending PowerShell with custom cmdlets,

Anonymous

# Unit testing Cmdlets for PowerShell @ Thursday, October 26, 2006 2:04 PM

I don&amp;#39;t use PowerShell myself, but this blog post caught my eye.If you miss VS, intellisense, TD.NET,

Anonymous

# Unit testing Cmdlets for PowerShell @ Thursday, October 26, 2006 2:06 PM

I don&amp;#39;t use PowerShell myself, but this blog post caught my eye.If you miss VS, intellisense, TD.NET,

Anonymous

# Kevin @ Wednesday, September 19, 2007 2:23 PM

http://30e63c3e91f7ebe25c1927567abb9077-t.uugowv.org <a href="http://30e63c3e91f7ebe25c1927567abb9077-h.uugowv.org">30e63c3e91f7ebe25c1927567abb9077</a> [url]http://30e63c3e91f7ebe25c1927567abb9077-b1.uugowv.org[/url] [url=http://30e63c3e91f7ebe25c1927567abb9077-b2.uugowv.org]30e63c3e91f7ebe25c1927567abb9077[/url] [u]http://30e63c3e91f7ebe25c1927567abb9077-b3.uugowv.org[/u] 514d733afec6f642f2a79083b4606e1b

Kyree

# re: Creating a PowerShell cmdlet @ Thursday, October 18, 2007 10:09 PM

Cool.

Alexios

# re: Creating a PowerShell cmdlet @ Friday, October 19, 2007 12:09 PM

Nice!

Martinos

# re: Creating a PowerShell cmdlet @ Friday, October 19, 2007 5:26 PM

Cool.

Iannis

# re: Creating a PowerShell cmdlet @ Friday, October 19, 2007 9:55 PM

Nice...

Metrophanes

# re: Creating a PowerShell cmdlet @ Saturday, October 20, 2007 9:40 AM

interesting

Fotis

# re: Creating a PowerShell cmdlet @ Saturday, October 20, 2007 2:53 PM

Nice...

Aniketos

# re: Creating a PowerShell cmdlet @ Sunday, October 21, 2007 12:00 AM

Cool!

Ilias

# re: Creating a PowerShell cmdlet @ Sunday, October 21, 2007 7:46 AM

Nice

Mamadshah

# vwsvrlka @ Friday, October 26, 2007 4:52 PM

[URL=http://ybebufhp.com]aftusjrq[/URL] eyxphign http://ulgwbngd.com gvemzaky khsfhibb <a href="http://srwtkbdu.com">kedxawsu</a>

vwsvrlka

# re: Creating a PowerShell cmdlet @ Thursday, November 01, 2007 7:13 AM

Nice

Konstandinos

# re: Creating a PowerShell cmdlet @ Thursday, November 01, 2007 7:13 AM

Interesting...

Nikolaos

# re: Creating a PowerShell cmdlet @ Thursday, November 01, 2007 8:03 PM

Interesting...

Elias

# re: Creating a PowerShell cmdlet @ Friday, November 02, 2007 2:17 AM

interesting

Emmanuel

# re: Creating a PowerShell cmdlet @ Friday, November 02, 2007 4:40 AM

Cool!

Konstantinos

# re: Creating a PowerShell cmdlet @ Friday, November 02, 2007 11:07 AM

Nice!

Orion

# re: Creating a PowerShell cmdlet @ Friday, November 02, 2007 11:38 AM

Cool.

Nikolaos

# re: Creating a PowerShell cmdlet @ Friday, November 02, 2007 3:13 PM

Nice...

Angelos

# re: Creating a PowerShell cmdlet @ Saturday, November 03, 2007 2:23 AM

Interesting...

Theologos

# re: Creating a PowerShell cmdlet @ Saturday, November 03, 2007 4:57 AM

Nice

Michalis

# re: Creating a PowerShell cmdlet @ Saturday, November 03, 2007 6:12 AM

Cool.

Agias

# re: Creating a PowerShell cmdlet @ Sunday, November 04, 2007 1:31 AM

Sorry :(

Nickolas

# re: Creating a PowerShell cmdlet @ Sunday, November 04, 2007 2:29 PM

Nice

Fanos

# re: Creating a PowerShell cmdlet @ Sunday, November 04, 2007 2:32 PM

interesting

Nikodemos

# re: Creating a PowerShell cmdlet @ Sunday, November 04, 2007 9:31 PM

Cool.

Andonios

# re: Creating a PowerShell cmdlet @ Monday, November 05, 2007 6:43 AM

Cool!

Kharilaos

# re: Creating a PowerShell cmdlet @ Monday, November 05, 2007 2:56 PM

interesting

Anastasios

# re: Creating a PowerShell cmdlet @ Tuesday, November 06, 2007 3:28 AM

Cool.

Giatas

# re: Creating a PowerShell cmdlet @ Tuesday, November 06, 2007 10:50 PM

Cool!

Nektarios

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 2:21 AM

Cool.

Panayiotis

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 7:32 AM

Cool...

Athan

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 8:52 AM

Nice!

Stephanos

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 9:31 AM

Cool.

Dino

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 6:13 PM

interesting

Emmanuel

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 6:16 PM

Sorry :(

Theofanis

# re: Creating a PowerShell cmdlet @ Wednesday, November 07, 2007 7:45 PM

Nice

Mamadshah

# re: Creating a PowerShell cmdlet @ Thursday, November 08, 2007 7:02 AM

Sorry :(

Dion

# re: Creating a PowerShell cmdlet @ Thursday, November 08, 2007 7:41 AM

Cool...

Photios

# re: Creating a PowerShell cmdlet @ Thursday, November 08, 2007 8:23 AM

Nice

Cosmo

# re: Creating a PowerShell cmdlet @ Thursday, November 08, 2007 9:47 PM

Nice!

Zenon

# ecpfmlnv @ Tuesday, February 05, 2008 11:38 AM

rtatshhr http://waiqbwbs.com acwglmch gkdssaob <a href="http://ngubmyav.com">ydjnwxkw</a> [URL=http://essgnwzk.com]ytgzhwzt[/URL]

ecpfmlnv

# re: Creating a PowerShell cmdlet @ Saturday, February 23, 2008 4:01 PM

onCO5C <a href="http://kkilpdenftga.com/">kkilpdenftga</a>, [url=http://pnahmvpzhfxg.com/]pnahmvpzhfxg[/url], [link=http://apxxdwqkqzjo.com/]apxxdwqkqzjo[/link], http://ztzuzmzotdlg.com/

srrekt

# re: Creating a PowerShell cmdlet @ Sunday, March 15, 2009 4:55 PM

fDlaiV <a href="http://ezxqpbmhzgtj.com/">ezxqpbmhzgtj</a>, [url=http://bruroazxmgzk.com/]bruroazxmgzk[/url], [link=http://dfvfnyhktfog.com/]dfvfnyhktfog[/link], http://uewqhejuruth.com/

ykkcajvjc