Step-by-Step C# PowerShell Module Creation in Visual Code

Prerequisites

You need to have at least the next tools:

  • Visual Studio Code
  • .NET Framework Developer Pack 4.7.2
  • PowerShell 5.1

Create a Class Library Project in the .NET Framework

The first step is to create a Class Library in the .NET Framework 4.7.2, compatible with PowerShell 5.1 generating the MyPowerShellModule.csproj file.

Open a PowerShell terminal and create the project:

mkdir MyPowerShellModule
cd MyPowerShellModule
dotnet new classlib -n MyPowerShellModule --framework net472
cd MyPowerShellModule

Configure the Project

We modify the csproj file to not have reference to .NET Core (maximum compatibility with PowerShell 5.1) and to use System.Management.Automation.dll directly from the Global Assembly Cache (GAC)

If you’re using Windows Server, verify that the System.Management.Automation.dll path is correct.

Open the MyPowerShellModule.csproj file  in VS Code and edit the XML contents:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <OutputType>Library</OutputType>
    <AssemblyName>MyPowerShellModule</AssemblyName>
    <RootNamespace>MyPowerShellModule</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System.Management.Automation">
<HintPath>C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

Write a PowerShell Cmdlet in C#

Open the Class1.cs file, rename it to GetWorldCmdLet.cs, and paste the code:

using System;
using System.Management.Automation;
namespace MyPowerShellModule
{
    [Cmdlet(VerbsCommon.Get, "World")]
    [OutputType(typeof(string))]
    public class GetWorldCmdLet : PSCmdlet
    {
        [Parameter(Mandatory = true)]
        public string Name { get; set; }

        protected override void ProcessRecord()
        {
            WriteObject($"Hello world! I’m {Name}");
        }
    }
}

Compile the DLL

We generate the dll in bin/Release/net472/MyPowerShellModule.dll to use in our module

dotnet build --configuration Release

Create the PowerShell Module

Now we need to create a PowerShell module based on the DLL.

mkdir "C:\Program Files\WindowsPowerShell\Modules\MyPowerShellModule"
cd "C:\Program Files\WindowsPowerShell\Modules\MyPowerShellModule"
cp "C:\Path\To\Project\bin\Release\net472\MyPowerShellModule.dll"

Create a MyPowerShellModule.psd1 file in the folder containing:

@{
    ModuleVersion = '1.0.0'
    GUID = '00000000-0000-0000-0000-000000000000'
    Author = 'Your Name'
    CompanyName = 'Your Company'
    Description = 'PowerShell module written in C#'
    PowerShellVersion = '5.1'
    RootModule = 'MyPowerShellModule.dll'
    FormatsToProcess = @()
    NestedModules = @()
    FunctionsToExport = @()
    CmdletsToExport = 'Get-World'
    VariablesToExport = @()
    AliasesToExport = @()
}

Leave a comment

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