Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- // Place your snippets for powershell here. Each snippet is defined under a snippet name and has a prefix, body and
- // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
- // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
- // same ids are connected.
- // Example:
- // "Print to console": {
- // "prefix": "log",
- // "body": [
- // "console.log('$1');",
- // "$2"
- // ],
- // "description": "Log output to console"
- // }
- "Captured ForEach": {
- "prefix": "foreach-capture",
- "description": "A captured foreach loop.",
- "body": [
- "\\$all$1 = foreach (\\$${2:item} in \\$${1:items}) {",
- "\t$0",
- "}"
- ]
- },
- "Comment Block (Better)": {
- "prefix": "comment-help-better",
- "description": "A better 'comment-based' help snippet.",
- "body": [
- "<#",
- "\t.SYNOPSIS",
- "\t\t${1:synopsis}",
- "\t.DESCRIPTION",
- "\t\t${2:description}",
- "\t",
- "\t.PARAMETER ${3:parameter}",
- "\t\t${4:parameter1}",
- "\t",
- "\t.INPUTS",
- "\t\t${5:Does not accept pipeline input.}",
- "\t.OUTPUTS",
- "\t\t${6:None.}",
- "\t",
- "\t.EXAMPLE",
- "\t\t${7:example}",
- "\t",
- "\t.NOTES",
- "\t\t${8:notes}",
- "#>"
- ]
- },
- "Current Directory Variable": {
- "prefix": "curDir-p2",
- "description": "Creates a variable to define the current directory of the running script.",
- "body": [
- "if (\\$null -eq (Get-Variable -Name 'PSScriptRoot' -ErrorAction 'SilentlyContinue')) {",
- "\t\\$PSScriptRoot = Split-Path -Path \\$MyInvocation.MyCommand.Definition -Parent",
- "}",
- "$0"
- ]
- },
- "Dynamic Parameter": {
- "prefix": "dynamic-parameter",
- "description": "Create a basic framework for DynamicParameters",
- "body": [
- "[CmdletBinding()]",
- "param()",
- "DynamicParam",
- "{",
- "\t# Use this block for defining the ValidateSet variables\r\n\r\n",
- "\t#######################################################",
- "\t\\$pName = \"${1:ParameterName}\";",
- "\t\\$attCol = New-Object 'System.Collections.ObjectModel.Collection[System.Attribute]';",
- "\t\\$pAtt = New-Object System.Management.Automation.ParameterAttribute -Property @{",
- "\t\tMandatory = \\$true",
- "\t\tPosition = 0",
- "\t};",
- "\t\\$attCol.Add(\\$pAtt);",
- "\t\\$alias = New-Object System.Management.Automation.AliasAttribute(\"\");",
- "\t\\$attCol.Add(\\$alias);",
- "\t\\$valSet = \"$2\"# Array",
- "\t\\$attCol.Add(\\$valSet);",
- "\t\\$rtParam = New-Object System.Management.Automation.RuntimeParameter(\\$pName, \\$([${3:Type}]), \\$attCol);",
- "\t\\$rtDict = New-Object System.Management.Automation.RuntimeParameterDictionary;",
- "\t\\$rtDict.Add(\\$pName, \\$rtParam);",
- "}",
- "Begin",
- "{",
- "\t\\$${1} = \\$PSBoundParameters[\"${1}\"];",
- "}",
- "Process",
- "{\r\n",
- "}"
- ]
- },
- "For Loop - WriteProgress": {
- "prefix": "for-write-progress",
- "description": "My 'for' loop.",
- "body": [
- "for (\\$${1:i} = ${2:1}; \\$$1 -${3:le} \\$${4:array}.${5:Count}; \\$$1++) {\r\n",
- "\t\\$${6:item} = \\$$4[\\$$1 - 1]",
- "\t\\$${7:progArgs} = @{",
- "\t\tActivity = \"${8:Processing $6s}\"",
- "\t\tStatus = \"Processing $6 \\$$1/\\$(\\$$4.$5)...$9\"",
- "\t\tId = ${10:0}",
- "\t\tPercentComplete = ((\\$$1/\\$(\\$$4.Count))*100)",
- "\t}\r\n",
- "\tWrite-Progress @progArgs\r\n",
- "\t$0",
- "}"
- ]
- },
- "Function No-Param": {
- "prefix": "func-noparam",
- "description": "A 'Function' block with no param block.",
- "body": [
- "Function ${1:functionName}($0) {",
- "\t",
- "}"
- ]
- },
- "Multi-line String": {
- "prefix": "multi-line-string",
- "description": "Creates a new variable for a 'here'-string.",
- "body": [
- "\\$${1:line} = @'",
- "$0",
- "$2'@"
- ]
- },
- "New Object": {
- "prefix": "newo",
- "description": "New-Object snippet",
- "body": "New-Object -TypeName \"$0\""
- },
- "New Object - ArgumentList": {
- "prefix": "newo-arg",
- "description": "New Object with Argument List snippet",
- "body": "New-Object -TypeName \"$1\" -ArgumentList $0"
- },
- "New Script Layout": {
- "prefix": "new-script-layout",
- "description": "Creates the layout of a self-contained script with regions denoting each section",
- "body": [
- "<#\r\n\t.SYNOPSIS\r\n\t\t\r\n",
- "\t.DESCRIPTION",
- "\t\t\r\n\t\t\r\n\t\tAuthor:\tMike Garvey\r\n\t\tDate:\t$CURRENT_MONTH/$CURRENT_DATE/$CURRENT_YEAR_SHORT\r\n",
- "\t.EXAMPLE\r\n\t\t\r\n",
- "\t.PARAMETER \r\n\t\t\r\n",
- "#>",
- "[CmdletBinding()]",
- "param",
- "(",
- "\t",
- ")\r\n",
- "#region ---- FUNCTIONS ----\r\n",
- "Function $0()\r\n{",
- "\t\r\n}\r\n",
- "\r\n\r\n#endregion\r\n\r\n",
- "#region ---- PRE-PROCESSING ----\r\n\r\n\r\n#endregion\r\n\r\n",
- "#region ---- SCRIPT BODY ----\r\n\r\n\r\n#endregion\r\n"
- ]
- },
- "Out-String Object": {
- "prefix": "out-string-obj",
- "description": "Pipes the specified object to an Out-String.",
- "body": "\\$(\\$$1 | Out-String)"
- },
- "Parameter-String": {
- "prefix": "param-string",
- "description": "Creates a string-type function parameter.",
- "body": [
- "[Parameter(Mandatory=\\$${1:false})]",
- "[string] \\$$0"
- ]
- },
- "Parameter-Typeless": {
- "prefix": "param-typeless",
- "description": "Creates a type-less (to be defined) function parameter.",
- "body": [
- "[Parameter(Mandatory=\\$${1:false})]",
- "[${2:object}] \\$$0"
- ]
- },
- "PSBoundParameter - ContainsKey": {
- "prefix": "psb-contains",
- "description": "Adds an \"if (\\$PSBoundParameters.ContainsKey()\" positive condition block",
- "body": [
- "if (\\$PSBoundParameters.ContainsKey(\"$1\"))",
- "{",
- "\t$0",
- "}"
- ]
- },
- "PSBoundParameters - NotContainsKey": {
- "prefix": "psb-nocontains",
- "description": "Adds an \"if (\\$PSBoundParameters.ContainsKey()\" negative condition block",
- "body": [
- "if (-not \\$PSBoundParameters.ContainsKey(\"$1\"))",
- "{",
- "\t$0",
- "}"
- ]
- },
- "PSBoundParameters - ContainsKey ElseIf": {
- "prefix": "psb-contains-elseif",
- "description": "Adds an \"elseif (\\$PSBoundParameters.ContainsKey()\" positive condition block",
- "body": [
- "elseif (\\$PSBoundParameters.ContainsKey(\"$1\"))",
- "{",
- "\t$0",
- "}"
- ]
- },
- "PSBoundParameters - NotContainsKey ElseIf": {
- "prefix": "psb-nocontains-elseif",
- "description": "Adds an \"elseif (\\$PSBoundParameters.ContainsKey()\" negative condition block",
- "body": [
- "elseif (-not \\$PSBoundParameters.ContainsKey(\"$1\"))",
- "{",
- "\t$0",
- "}"
- ]
- },
- "Script CmdletBinding": {
- "prefix": "param",
- "description": "Create the CmdletBinding and Param block for a PSScript file.",
- "body": [
- "[CmdletBinding(SupportsShouldProcess=\\$${1:false}, ConfirmImpact=\"${2:None}\")]",
- "param (",
- "\t$0",
- ")",
- "Begin {",
- "\t",
- "}",
- "Process {",
- "\t",
- "}"
- ]
- },
- "String IsNullOrEmpty": {
- "prefix": "string-nullorempty",
- "description": "String is null or empty",
- "body": "[string]::IsNullOrEmpty($0)"
- },
- "String NOT IsNullOrEmpty": {
- "prefix": "string-not-nullorempty",
- "description": "String is NOT null or empty.",
- "body": "-not [string]::IsNullOrEmpty($0)"
- },
- "String IsNullOrWhitespace": {
- "prefix": "string-nullorwhitespace",
- "description": "String is null, empty, or whitespace",
- "body": "[string]::IsNullOrWhitespace($0)"
- },
- "String NOT IsNullOrWhitespace": {
- "prefix": "string-not-nullorwhitespace",
- "description": "String is NOT null, empty, or whitespace",
- "body": "-not [string]::IsNullOrWhitespace($0)"
- },
- "Write Host Double With Colour": {
- "prefix": "write-host-double",
- "description": "Write-Host (double quotes) with ForegroundColor specified.",
- "body": "Write-Host \"$0\" -ForegroundColor ${1|Black,Blue,Cyan,DarkBlue,DarkCyan,DarkGray,DarkGreen,DarkMagenta,DarkRed,DarkYellow,Gray,Green,Magenta,Red,White,Yellow|}"
- },
- "Write Host Single With Colour": {
- "prefix": "write-host-single",
- "description": "Write-Host (single quotes) with ForegroundColor specified.",
- "body": "Write-Host '$0' -ForegroundColor ${1|Black,Blue,Cyan,DarkBlue,DarkCyan,DarkGray,DarkGreen,DarkMagenta,DarkRed,DarkYellow,Gray,Green,Magenta,Red,White,Yellow|}"
- }
- }
Add Comment
Please, Sign In to add comment