Tip: In VS Code type ‘ex-cmdlet’ [Enter] to get comment header and function syntax

In case you haven’t come across this, I just found this handy snippet in the PowerShell extension for Visual Studio Code. Type ex-cmdlet followed by Enter to get all the code for a function with full comment header (and you can delete the function code to just use the comment header for a script):

<# .SYNOPSIS Short description .DESCRIPTION Long description .EXAMPLE Example of how to use this cmdlet .EXAMPLE Another example of how to use this cmdlet .INPUTS Inputs to this cmdlet (if any) .OUTPUTS Output from this cmdlet (if any) .NOTES General notes .COMPONENT The component this cmdlet belongs to .ROLE The role this cmdlet belongs to .FUNCTIONALITY The functionality that best describes this cmdlet #> function Verb-Noun { [CmdletBinding(DefaultParameterSetName='Parameter Set 1', SupportsShouldProcess=$true, PositionalBinding=$false, HelpUri = 'http://www.microsoft.com/', ConfirmImpact='Medium')] [Alias()] [OutputType([String])] Param ( # Param1 help description [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ValueFromRemainingArguments=$false, ParameterSetName='Parameter Set 1')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [ValidateCount(0,5)] [ValidateSet("sun", "moon", "earth")] [Alias("p1")] $Param1, # Param2 help description [Parameter(ParameterSetName='Parameter Set 1')] [AllowNull()] [AllowEmptyCollection()] [AllowEmptyString()] [ValidateScript({$true})] [ValidateRange(0,5)] [int] $Param2, # Param3 help description [Parameter(ParameterSetName='Another Parameter Set')] [ValidatePattern("[a-z]*")] [ValidateLength(0,15)] [String] $Param3 ) begin { } process { if ($pscmdlet.ShouldProcess("Target", "Operation")) { } } end { } } 

And for just a basic function you can just type cmdlet [Enter]:

function Verb-Noun { [CmdletBinding()] param ( ) begin { } process { } end { } } 

There are several other snippets available, with some listed here:


(That article tells you to download a .json file and save it in a local Snippets folder, but on the latest version of VS Code / PowerShell extension I didn’t have to do that, so it looks like it’s done for you).

submitted by /u/Tonedefff
[link] [comments]

Leave a Reply