Building a C# Cmdlet with Visual Studio
This document describes steps for building a C# Cmdlet with Visual Studio in 2 ways:
Building a C# Cmdlet for PowerShell Core with Visual Studio
This demonstrates how to build your own C# cmdlet for PowerShell Core with Visual Studio. Targeting for PowerShell Core means that the cmdlet may not work against Windows PowerShell if you take dependencies on new APIs introduced in PowerShell Core.
We will use the free Visual Studio Community 2017.
When installing Visual Studio 2017 select
.NET Core cross-platform developmentunderOther Toolsets
Create new C# project
SendGreetingof typeClass Library (.NET Core)
Now we need to setup PowerShell Core reference assemblies. In
Solution Explorerright click on projectDependenciesand selectManage NuGet Packages...In the top-right corner of the package manager click on the smallSettingssprocket icon that is to the right fromPackage sourcedropdown. By default, there will be onlynuget.orgpackage source inAvailable package sourceslist. Add another package source with namepowershell-coreand sourcehttps://powershell.myget.org/F/powershell-core/api/v3/index.json
In the package manager select new
powershell-coreinPackage sourcedropdown, selectBrowsetab, type inSystem.Management.Automationin the search and selectInclude prerelease. It should findSystem.Management.Automationpackage, select it and it will show package details; install it usingInstallbutton.
Add the code of cmdlet:
using System.Management.Automation; // PowerShell namespace. namespace SendGreeting { // Declare the class as a cmdlet and specify and // appropriate verb and noun for the cmdlet name. [Cmdlet(VerbsCommunications.Send, "Greeting")] public class SendGreetingCommand : Cmdlet { // Declare the parameters for the cmdlet. [Parameter(Mandatory = true)] public string Name { get; set; } // Override the ProcessRecord method to process // the supplied user name and write out a // greeting to the user by calling the WriteObject // method. protected override void ProcessRecord() { WriteObject("Hello " + Name + "!"); } } }Build solution (F6); The
Outputwindow will print the location of generated cmdlet DLL:
Start PowerShell Core, run
Import-Moduleon DLL path from previous step and run cmdlet:
You can also run the same cmdlet on Linux and other systems that PowerShell Core supports:

Building a C# Cmdlet for PowerShell Standard 3.0 with Visual Studio
Steps below show how to build your own C# cmdlet for PowerShell Standard 3.0 with Visual Studio. Targeting PowerShell Standard 3.0 means that the same module will work against PowerShell Core as well as Windows PowerShell v3 and newer, however, you are limited to a subset of the available PowerShell APIs.
We will use the free Visual Studio Community 2017.
When installing Visual Studio 2017 select
.NET Core cross-platform developmentunderOther Toolsets
Create new C# project
SendGreetingStdof typeClass Library (.NET Standard)
On project properties verify that
Target frameworkis.NET Standard 2.0:

Now we need to setup reference assemblies. In
Solution Explorerright click on projectDependenciesand selectManage NuGet Packages...In the top-right corner of the package manager selectnuget.orgpackage source, selectBrowsetab, type inPowerShellStandard.Libraryin the search and selectInclude prerelease. It should findPowerShellStandard.Librarypackage, select it and it will show package details; install it usingInstallbutton.
Add the code of cmdlet:
using System.Management.Automation; // PowerShell namespace. namespace SendGreeting { // Declare the class as a cmdlet and specify and // appropriate verb and noun for the cmdlet name. [Cmdlet(VerbsCommunications.Send, "Greeting")] public class SendGreetingCommand : Cmdlet { // Declare the parameters for the cmdlet. [Parameter(Mandatory = true)] public string Name { get; set; } // Override the ProcessRecord method to process // the supplied user name and write out a // greeting to the user by calling the WriteObject // method. protected override void ProcessRecord() { WriteObject("Hello " + Name + "!"); } } }Build solution (F6); The
Outputwindow will print the location of generated cmdlet DLL:
Now cmdlet can be run on systems supported by PowerShell Standard;
For example:
On PowerShell Core on Windows:
On PowerShell Core on Linux:
On Windows PowerShell on Windows (this requires .NET Framework 4.7.1


