PowerShell Binary Module development

You can write the power shell modules through scripts (Script Modules) or through c# code (Binary modules)

Script modules can have any valid PS code and save as .psm1
Binary modules can be any assembly that contain cmdlet classes (.dll) the assembly would be imported to powershell and use the commands

In this post am writing about the how to create a Binary PowerShell module

Open Visual Studio 2012
New Project -> Class Library

In order to get the powershell runspace inside the programming language and to create the powershell commands we must

add reference of “System.management.automation” dll

The DLL would be available in C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll ( Windows 10 / 8.1)

Then create a powershell function in the code
First extend the Cmdlet in your class

public class SampleGet : Cmdlet

Add verbs as Get / Set

If you want to use parameters add it and set whether its mandatory or not

In order to get the above cmdlet to do something we’ll need to override one of the processing methods.

Processing Methods In PowerShell Module

  • BeginProcessing – Executed once when the cmdlet starts. Pipelined objects will not be available during the execution of this method. It’s regarded as best practice, if a cmdlet doesn’t accept pipeline input, to do all processing in this method.
  • ProcessRecord – Executed once per pipelined object.
  • EndProcessing – Executed once when processing ends.

Here is the code for sample powershell module

See the gist on github.

Build the project and import the dll

Importing Module

PS C:\WINDOWS\system32> Import-Module C:\Users\Vignesh\Desktop\PSModule\SamplePsModule\SamplePsModule\bin\Debug\SamplePsModule.dll
Get the List of commands implemented

PS C:\WINDOWS\system32> Get-Command -Module SamplePsModule

Run Command

PS C:\WINDOWS\system32> Get-Vignesh -name vignesh -id 1 -category dev | Format-List

Ways to write the response / message

  • WriteObject – Writes an object to the pipeline.
  • WriteError – Writes a new error record to the host.
  • WriteDebug – Writes a debug message.
  • WriteVerbose – Writes a verbose message.
  • WriteProgress – Writes a progress message.
  • WriteWarning – Writes a warning message.

Parameter Attributes

  • Mandatory – If set to true, the property is mandatory for the specified parameter set.It will promt even if it is not added with command parameter
  • ParameterSetName – If set, the parameter is part of the specified parameter set. Otherwise it is part of the default parameter set.
  • Position – The position in the parameter set where the parameter resides if you don’t specify parameter names and simply use position of arguments.
  • ValueFromPipeline – Objects can be piped into the property as is.
    ValueFromPipelineByPropertyName – If the parameter name matches the name of a property of an object piped to this cmdlet, this parameter will be set to that value

This site uses Akismet to reduce spam. Learn how your comment data is processed.