This site uses advanced css techniques
Those who create PowerShell cmdlets—who I assume are using Visual Studio— surely find themselves typing the same set of code every time the make a new .cs source file to hold a new cmdlet, and I tired of this as well, so finally (after years!) got around to creating a proper new-item template for Visual Studio.
Whoa - SO NICE.
This ended up being far easier than I expected so am documenting it here; I wish I'd have done this a long time ago.
These instructions work for Visual Studio 2017 but believe they apply more widely.
Templates are provided as a ZIP file—such as PowerShell Cmdlet.zip— and are dropped into the $env:USERPROFILE\Visual Studio 2017\Templates\ItemTemplates\ directory.
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item"> <TemplateData> <DefaultName> My_Cmdlet.cs </DefaultName> <Name> My Powershell Cmdlet </Name> <Description> Steve's PowerShell cmdlet template </Description> <ProjectType>CSharp</ProjectType> <SortOrder>10</SortOrder> <Icon> __TemplateIcon.ico </Icon> </TemplateData> <TemplateContent> <References> <Reference> <Assembly> System.Management.Automation </Assembly> </Reference> </References> <ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true"> PowershellCmdlet.cs </ProjectItem> </TemplateContent> </VSTemplate>
// $itemname$.cs // // Written by Steve with lots of awesome using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management.Automation; namespace $rootnamespace$ { [Cmdlet(VerbsCommon.Get, "Item")] public class Get_Item : PSCmdlet { } }
Put these two items in a ZIP file, navigate to your Documents folder, then drill down to the Visual Studio 2017\Templates\ItemTemplates\ directory, and drop the ZIP file there.
Then, when you restart Visual Studio the next time, it's available in the Add New Item:
I've highlighted in red how items in the template show up in the output add-new-item dialog, so you can tell what you're adjusting.
This has been super helpful for as many cmdlets as I write.
First published: 2019/07/29