Share via


Create Your Own PowerShell Commands using C# Class Library

Hi There, Can I create my own PowerShell commands.

Solution

Yes, use one of the below option
Create a C# Class Library and consume the DLL as module in PowerShell
Create your own PowerShell module.

Steps

1. Open Visual Studio as Administrator
2. Click File - New - Project and choose Visual C# - Windows - Class Library
3. Name the file as required.
4. Delete the default class library [Class1.cs]
5. Right Click on Solution file and choose Add. Select New Item and create a new Class File
6. The Code Block looks similar to the below. Where CustomCommand is my new class file name

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ClassLibrary1
{
    class CustomCommand
    {
    }
}
  1. Right click on References choose Add Reference and from .NET add "System.Management" and "System.Management.Automation"
  2. Alternatively You can refer to the DLL path of PowerShell.

You can use the below code for creating three custom commands:

  1. Clear-UserTemporaryFiles
  2. Clear-IEHistory
  3. Clear-TemporaryInternetFiles
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Management; 
using System.Management.Automation; 
using System.IO; 
  
namespace Windows_Management 
{ 
    [Cmdlet(VerbsCommon.Clear,"TemporaryInternetFiles")] 
    
    public class WindowsManagement : PSCmdlet 
    { 
        protected override void  ProcessRecord() 
        { 
            //Delete Internet Cache Files and Folders 
            string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache); 
            Console.ForegroundColor = ConsoleColor.DarkYellow; 
            Console.WriteLine("Clearing Temporary Internet Cache Files and Directories....." + path); 
            System.IO.DirectoryInfo folder = new DirectoryInfo(path); 
            foreach (FileInfo files in folder.GetFiles()) 
            { 
                try 
                { 
                    files.Delete(); 
                } 
                catch (Exception ex) 
                { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                 
            } 
             foreach(DirectoryInfo  Directory in folder.GetDirectories() ) 
            { 
                try 
                { 
                    Directory.Delete(); 
                } 
                catch (Exception ex) { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                   
            } 
            Console.WriteLine("Done Processing!!!"); 
            Console.ResetColor(); 
        } 
           
    } 
} 
  
namespace clearInternetexplorerHistory 
{ 
    [Cmdlet(VerbsCommon.Clear,"IEHistory")] 
    public class clearInternetexplorerHistory : PSCmdlet 
    { 
        protected override void  ProcessRecord() 
{ 
     // base.ProcessRecord(); 
            string path = Environment.GetFolderPath(Environment.SpecialFolder.History); 
            Console.ForegroundColor = ConsoleColor.DarkYellow; 
            Console.WriteLine("Clearing Internet Explorer History....." + path); 
            System.IO.DirectoryInfo folder = new DirectoryInfo(path); 
            foreach (FileInfo files in folder.GetFiles()) 
            { 
                try 
                { 
                    files.Delete(); 
                } 
                catch (Exception ex) 
                { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                 
            } 
             foreach(DirectoryInfo  Directory in folder.GetDirectories() ) 
            { 
                try 
                { 
                    Directory.Delete(); 
                } 
                catch (Exception ex) { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                   
            } 
            Console.WriteLine("Done Processing!!!"); 
            Console.ResetColor(); 
        } 
           
    } 
  
} 
      
  
      
namespace UserTemporaryFiles 
{ 
    [Cmdlet(VerbsCommon.Clear, "UserTemporaryFiles")] 
    public class UserTemporaryFiles : PSCmdlet 
    { 
        protected override void ProcessRecord() 
        { 
            //base.ProcessRecord(); 
              
            string temppath = System.IO.Path.GetTempPath(); 
            System.IO.DirectoryInfo usertemp = new DirectoryInfo(temppath); 
            Console.WriteLine("Clearing Your Profile Temporary Files..." + temppath); 
             
            foreach(FileInfo tempfiles in usertemp.GetFiles()) 
            { 
                try 
                { 
                    tempfiles.Delete(); 
                } 
                catch (Exception ex) { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                      
            } 
              
            Console.WriteLine("Done Processing!!!"); 
            foreach (DirectoryInfo tempdirectory in usertemp.GetDirectories()) 
            { 
                try 
                { 
                    tempdirectory.Delete(); 
                } 
                catch (Exception ex) 
                { 
                    System.Diagnostics.Debug.WriteLine(ex); 
                } 
                  
            } 
        } 
    } 
}

In the Solution package folder search for the DLL in \bin\debug\Somefilename.DLL and copy to the desired location in this case I copied to C:\Temp

PoweShell Code:

Import-Module "C:\Temp\Windows Management.dll"
Clear-TemporaryInternetFiles
Clear-IEHistory
Clear-UserTemporaryFiles

Please download the full Solution File from Technet Gallery