Powershell
cotrWhileLoop
PowerShell While Loop
while (${1:condition}) { # Your code here}
cotrEqual
PowerShell Equal To
-eq
cotrNotEqual
PowerShell Not Equal To
-ne
cotrTypesBoolTrue
PowerShell Boolean True
$true
cotrVarStringMulti
PowerShell Create Multi-Line String Variable
$${1:myString} = @"${2:Line 1}${3:Line 2}${4:Line 3}"@;
cotrTypeCompare
PowerShell Type Comparison
# Check if two variables have the same type:if (${1:variable1}.GetType() -eq ${2:variable2}.GetType()) { # Your code here}
cotrTypes
PowerShell Types
$BLOCK_COMMENT_STARTPowerShell 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
[double]
cotrPrintMulti
PowerShell Print Multi
Write-Host @"${1:Line 1}${2:Line 2}${3:Line 3}"@;
cotrOperators
PowerShell Mathematical Operators
# 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
$${1:myVar} = $2
cotrConst
PowerShell Create Constant
$${1:myConst} = $2
cotrInfo
PowerShell Info
# Typing: Dynamically typed# Paradigm: Object-oriented, imperative, scripting# Compilation: Interpreted# Concurrency: Supports multi-threading with the System.Threading namespace
cotrConcat
PowerShell Concatenate Strings
"${1:string1}" + "${2:string2}"
cotrFuncLambda
PowerShell Lambda Function (Script Block)
# 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
{${1:parameters} -> ${2:# Your code here}}
cotrVarBool
PowerShell Create Boolean Variable
$${1:myBool} = ${2:$true}
cotrVarList
PowerShell Create Array Variable
$${1:myArray} = @(${2:items})
cotrForLoop
PowerShell For Loop
for ($${1:i} = 0; $1 -lt ${2:10}; $1++) { # Your code here}
cotrOperatorsBool
PowerShell Boolean Operators
# 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
Get-Date
cotrIf
PowerShell If Statement
if (${1:condition}) { # Your code here}
cotrFuncArgs
PowerShell Function Args
# 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
throw '${1:Your message here}'
cotrTernary
PowerShell Ternary Operator
${1:condition} ? ${2:trueValue} : ${3:falseValue}
cotrFuncSyntax
PowerShell Function Syntax
# 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)
# 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
[datetime]
cotrVarNullable
PowerShell Create Nullable Variable
$${1:myVar} = $null
cotrVarDate
PowerShell Create Date Variable
$${1:myDate} = Get-Date -Year ${2:year} -Month ${3:month} -Day ${4:day}
cotrVarSyntax
PowerShell Variable Declaration Syntax
# 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
function ${2:MyFunction} { param( [Parameter(Mandatory)] [${3:type}] ${4:$arg1}, [Parameter(Mandatory)] [${5:type}] ${6:$arg2} ) ${7:# Your code here}}
cotrClass
PowerShell Class
class ${1:MyClass} { # Your code here}
cotrTypesString
PowerShell String Type
[string]
cotrForEachLoop
PowerShell For Each Loop
foreach ($${1:item} in ${2:iterable}) { # Your code here}
cotrFunc
PowerShell Function
function ${2:myFunction} { param ( ${3:parameters} )
${4:# Your code here}}
cotrInterpolate
PowerShell Interpolate String
"Your string here $${1:variable}"
cotrVarMap
PowerShell Create Hash Table Variable
$${1:myHashTable} = @{ ${2:key1} = ${3:value1} ${4:key2} = ${5:value2} # Add more key-value pairs here}
cotrFuncAnon
PowerShell Anonymous Function
{ param(${1:parameters}) ${2:# Your code here}}
cotrTypesBool
PowerShell Boolean Type
[bool]
cotrTypesBoolFalse
PowerShell Boolean False
$false
cotrTypesDynamic
PowerShell Dynamic Type
# PowerShell is a dynamically typed language.
cotrTypesInt
PowerShell Integer Type
[int]
cotrVarNum
PowerShell Create Double Variable
$${1:myDouble} = ${2:0.0}
cotrIfElse
PowerShell If Else Statement
if (${1:condition}) { # Your code here} else { # Your code here}
cotrVarStatic
PowerShell Static Variable
# 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
[array]
cotrTypesNull
PowerShell Null Type
$null
cotrVarTyped
PowerShell Create Typed Variable
[${1:Type}] $${2:myVar} = $3
cotrFuncArgs
PowerShell Function Arguments
# 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
${1:variable}.GetType()
cotrVarString
PowerShell Create String Variable
$${1:myString} = "${2:Your string here}"
cotrVarInt
PowerShell Create Integer Variable
$${1:myInt} = ${2:0}
cotrSwitch
PowerShell Switch Statement
switch (${1:variable}) { ${2:value1} { # Your code here break } ${3:value2} { # Your code here break } default { # Your code here }}
cotrGenMap
PowerShell Generate Map
# 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
# 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
[hashtable]
cotrEntry
PowerShell Entry Point
# PowerShell Entry Point# To run this script, use: `powershell filename.ps1`
Write-Host 'Hello, World!'
cotrComment
PowerShell Comment
# ${1:Your comment here}
cotrGenList
PowerShell Generate List
1..${1:length} | ForEach-Object { ${2:'item'} + $_ }
cotrPrint
PowerShell Print
Write-Host ${1:'Your message here'}
cotrCommentMulti
PowerShell Multi-Line Comment
<#${1:Your comment here}#>
cotrTryCatch
PowerShell Try Catch
try { # Your code here} catch { # Your code here}