Skip to content

Powershell

cotrWhileLoop

PowerShell While Loop

Terminal window
while (${1:condition}) {
# Your code here
}

cotrEqual

PowerShell Equal To

Terminal window
-eq

cotrNotEqual

PowerShell Not Equal To

Terminal window
-ne

cotrTypesBoolTrue

PowerShell Boolean True

Terminal window
$true

cotrVarStringMulti

PowerShell Create Multi-Line String Variable

Terminal window
$${1:myString} = @"
${2:Line 1}
${3:Line 2}
${4:Line 3}
"@;

cotrTypeCompare

PowerShell Type Comparison

Terminal window
# Check if two variables have the same type:
if (${1:variable1}.GetType() -eq ${2:variable2}.GetType()) {
# Your code here
}

cotrTypes

PowerShell Types

Terminal window
$BLOCK_COMMENT_START
PowerShell is a dynamically typed language.
Some common types in PowerShell include:
- [int]: Integer
- [double]: Double-precision floating-point number
- [string]: String
- [bool]: Boolean
- [array]: Array
- [hashtable]: Hash table (associative array)
- [datetime]: Date and time
- [PSCustomObject]: Custom object
- [System.Collections.Hashtable]: .NET Hashtable
- [System.Collections.Generic.List[T]]: .NET Generic List
- [System.Management.Automation.PSObject]: PowerShell object
$BLOCK_COMMENT_END

cotrTypesNum

PowerShell Double Type

Terminal window
[double]

cotrPrintMulti

PowerShell Print Multi

Terminal window
Write-Host @"
${1:Line 1}
${2:Line 2}
${3:Line 3}
"@;

cotrOperators

PowerShell Mathematical Operators

Terminal window
# PowerShell Mathematical Operators
# Addition: +
# Subtraction: -
# Multiplication: *
# Division: /
# Modulus (Remainder): %
# Exponentiation: Not directly supported, use [Math]::Pow(base, exponent)
# Increment: Use += 1
# Decrement: Use -= 1
# Assignment: =
# Addition assignment: +=
# Subtraction assignment: -=
# Multiplication assignment: *=
# Division assignment: /=
# Modulus assignment: %=

cotrVar

PowerShell Create Variable

Terminal window
$${1:myVar} = $2

cotrConst

PowerShell Create Constant

Terminal window
$${1:myConst} = $2

cotrInfo

PowerShell Info

Terminal window
# Typing: Dynamically typed
# Paradigm: Object-oriented, imperative, scripting
# Compilation: Interpreted
# Concurrency: Supports multi-threading with the System.Threading namespace

cotrConcat

PowerShell Concatenate Strings

Terminal window
"${1:string1}" + "${2:string2}"

cotrFuncLambda

PowerShell Lambda Function (Script Block)

Terminal window
# PowerShell does not have a 'lambda' keyword.
# Instead, script blocks are used as anonymous functions.
$lambda = {
param(${1:parameters})
${2:# Your code here}
}
# Usage
$result = $lambda.Invoke(${3:arguments})
Write-Host "Result: $result"

cotrFuncArrow

PowerShell Arrow Function

Terminal window
{${1:parameters} ->
${2:# Your code here}
}

cotrVarBool

PowerShell Create Boolean Variable

Terminal window
$${1:myBool} = ${2:$true}

cotrVarList

PowerShell Create Array Variable

Terminal window
$${1:myArray} = @(${2:items})

cotrForLoop

PowerShell For Loop

Terminal window
for ($${1:i} = 0; $1 -lt ${2:10}; $1++) {
# Your code here
}

cotrOperatorsBool

PowerShell Boolean Operators

Terminal window
# PowerShell Boolean Operators
# Logical AND: -and
# Logical OR: -or
# Logical NOT: -not
# Equality: -eq
# Inequality: -ne
# Greater than: -gt
# Less than: -lt
# Greater than or equal to: -ge
# Less than or equal to: -le

cotrNow

PowerShell Date Now

Terminal window
Get-Date

cotrIf

PowerShell If Statement

Terminal window
if (${1:condition}) {
# Your code here
}

cotrFuncArgs

PowerShell Function Args

Terminal window
# In PowerShell, functions can have arguments with default values.
function ${2:myFunction} {
param (
[${3:Type}] $${4:arg1},
[${5:Type}] $${6:arg2} = ${7:defaultValue}
)
${8:# Your code here}
}

cotrThrow

PowerShell Throw Exception

Terminal window
throw '${1:Your message here}'

cotrTernary

PowerShell Ternary Operator

Terminal window
${1:condition} ? ${2:trueValue} : ${3:falseValue}

cotrFuncSyntax

PowerShell Function Syntax

Terminal window
# PowerShell Function Syntax
# Basic function:
# function functionName {
# param (
# parameters
# )
# # Your code here
# }
# Function with arguments:
# function functionName {
# param (
# [Type] $arg1,
# [Type] $arg2,
# ...
# )
# # Your code here
# }

cotrStructure

PowerShell Project Structure (High-Level)

Terminal window
# Recommended High-Level PowerShell Project Structure:
# - /
# - README.md: Documentation about the project.
# - .ps1 files: Individual PowerShell script files.
# - /src
# - Contains PowerShell modules or scripts organized by functionality.
# - /tests
# - Contains Pester tests for testing your PowerShell scripts and modules.
# - /lib
# - Optional: Contains third-party libraries or scripts used in the project.
# - /docs
# - Optional: Additional documentation, help files, or related documents.
# - /tools
# - Optional: Utility and helper scripts that assist with project tasks.
# - /output
# - Optional: A location to store output files generated by scripts.
# Note:
# - Organize .ps1 script files and modules in the src folder for clarity.
# - Use descriptive names for script files to indicate their purpose or function.

cotrTypesDate

PowerShell Date Type

Terminal window
[datetime]

cotrVarNullable

PowerShell Create Nullable Variable

Terminal window
$${1:myVar} = $null

cotrVarDate

PowerShell Create Date Variable

Terminal window
$${1:myDate} = Get-Date -Year ${2:year} -Month ${3:month} -Day ${4:day}

cotrVarSyntax

PowerShell Variable Declaration Syntax

Terminal window
# PowerShell Variable Declaration Syntax:
# - $: (Scope: Global or Function)
# - Variables start with a dollar sign.
# - Can be reassigned and redeclared within their scope.
# - Use with caution due to potential scoping issues.
# - const: (Scope: Global)
# - Cannot be reassigned or redeclared.
# - Use for values that should remain constant.
# Note:
# - PHP does not have a direct equivalent to 'let'.
# - Use '$' for most variable declarations.
# - Use 'const' for values that are known at compile time.

cotrFuncArgsNamed

PowerShell Named Arguments Function

Terminal window
function ${2:MyFunction} {
param(
[Parameter(Mandatory)]
[${3:type}] ${4:$arg1},
[Parameter(Mandatory)]
[${5:type}] ${6:$arg2}
)
${7:# Your code here}
}

cotrClass

PowerShell Class

Terminal window
class ${1:MyClass} {
# Your code here
}

cotrTypesString

PowerShell String Type

Terminal window
[string]

cotrForEachLoop

PowerShell For Each Loop

Terminal window
foreach ($${1:item} in ${2:iterable}) {
# Your code here
}

cotrFunc

PowerShell Function

Terminal window
function ${2:myFunction} {
param (
${3:parameters}
)
${4:# Your code here}
}

cotrInterpolate

PowerShell Interpolate String

Terminal window
"Your string here $${1:variable}"

cotrVarMap

PowerShell Create Hash Table Variable

Terminal window
$${1:myHashTable} = @{
${2:key1} = ${3:value1}
${4:key2} = ${5:value2}
# Add more key-value pairs here
}

cotrFuncAnon

PowerShell Anonymous Function

Terminal window
{
param(${1:parameters})
${2:# Your code here}
}

cotrTypesBool

PowerShell Boolean Type

Terminal window
[bool]

cotrTypesBoolFalse

PowerShell Boolean False

Terminal window
$false

cotrTypesDynamic

PowerShell Dynamic Type

Terminal window
# PowerShell is a dynamically typed language.

cotrTypesInt

PowerShell Integer Type

Terminal window
[int]

cotrVarNum

PowerShell Create Double Variable

Terminal window
$${1:myDouble} = ${2:0.0}

cotrIfElse

PowerShell If Else Statement

Terminal window
if (${1:condition}) {
# Your code here
} else {
# Your code here
}

cotrVarStatic

PowerShell Static Variable

Terminal window
# PowerShell does not have static variables in the same way as some other languages.
# You can use class variables or module variables to achieve similar functionality.

cotrTypesList

PowerShell List Type

Terminal window
[array]

cotrTypesNull

PowerShell Null Type

Terminal window
$null

cotrVarTyped

PowerShell Create Typed Variable

Terminal window
[${1:Type}] $${2:myVar} = $3

cotrFuncArgs

PowerShell Function Arguments

Terminal window
# In PowerShell, functions can have arguments with default values.
function ${2:myFunction} {
param (
[${3:Type}] $${4:arg1},
[${5:Type}] $${6:arg2} = ${7:defaultValue}
)
${8:# Your code here}
}

cotrTypeCheck

PowerShell Type Check

Terminal window
${1:variable}.GetType()

cotrVarString

PowerShell Create String Variable

Terminal window
$${1:myString} = "${2:Your string here}"

cotrVarInt

PowerShell Create Integer Variable

Terminal window
$${1:myInt} = ${2:0}

cotrSwitch

PowerShell Switch Statement

Terminal window
switch (${1:variable}) {
${2:value1} {
# Your code here
break
}
${3:value2} {
# Your code here
break
}
default {
# Your code here
}
}

cotrGenMap

PowerShell Generate Map

Terminal window
# PowerShell does not have a built-in way to generate a map with a specific number of key-value pairs.
# You can use a loop or a custom function to achieve this.

cotrTypeConvert

PowerShell Type Conversion

Terminal window
# PowerShell Type Conversion:
# Explicit conversions:
# - [Type]variable // Type casting (can throw an error if conversion fails)
# - variable.ToString() // Converts to string
# - [int]variable // Converts to integer
# - [double]variable // Converts to double
# - ... (Various conversion methods and type accelerators)
# Note:
# - Be cautious with type casting, as it can lead to errors if the conversion is not valid.

cotrTypesMap

PowerShell Map Type

Terminal window
[hashtable]

cotrEntry

PowerShell Entry Point

Terminal window
# PowerShell Entry Point
# To run this script, use: `powershell filename.ps1`
Write-Host 'Hello, World!'

cotrComment

PowerShell Comment

Terminal window
# ${1:Your comment here}

cotrGenList

PowerShell Generate List

Terminal window
1..${1:length} | ForEach-Object { ${2:'item'} + $_ }

cotrPrint

PowerShell Print

Terminal window
Write-Host ${1:'Your message here'}

cotrCommentMulti

PowerShell Multi-Line Comment

Terminal window
<#
${1:Your comment here}
#>

cotrTryCatch

PowerShell Try Catch

Terminal window
try {
# Your code here
} catch {
# Your code here
}