Skip to content

Instantly share code, notes, and snippets.

@jorgeasaurus
Created October 2, 2024 04:00
Show Gist options
  • Save jorgeasaurus/f4eb52a4fa9cd7b788963d2cf8d46498 to your computer and use it in GitHub Desktop.
Save jorgeasaurus/f4eb52a4fa9cd7b788963d2cf8d46498 to your computer and use it in GitHub Desktop.
# Output
Write-Output "Hello, World!" # Outputs: Hello, World!
# Variables
$x = 5 # Integer variable
$name = "Alice" # String variable
$a, $b, $c = 1, 2, 3 # Multiple assignments
# Data Types
$integer = 10 # [int]
$float = 3.14 # [double]
$string = "Hello" # [string]
$boolean = $true # [bool]
$array = @(1, 2, 3) # Array
$hashtable = @{name = "Alice"; age = 25 } # Hashtable (similar to dict)
$set = [System.Collections.Generic.HashSet[object]]::new($array) # Set
# Control Flow
if ($x -gt 0) {
Write-Output "x is positive"
}
elseif ($x -eq 0) {
Write-Output "x is zero"
}
else {
Write-Output "x is negative"
}
for ($i = 0; $i -lt 5; $i++) {
Write-Output $i # Loops through numbers 0 to 4
}
while ($x -lt 10) {
$x++ # Increment by 1
}
# Functions
function My-Function {
return "Hello from the function!"
}
# String Operations
$upper = "Hello".ToUpper() # "HELLO"
$lower = "HELLO".ToLower() # "hello"
$split = "Hello World" -split " " # ['Hello', 'World']
$join = -join ('a', 'b', 'c') # 'abc'
$replace = "Hello" -replace "H", "J" # "Jello"
$length = "Hello".Length # 5
# Array/List Operations
$array += 4 # Adds 4 to $array
$array = $array -ne 2 # Removes 2 from $array
$array = $array | Sort-Object # Sorts $array in ascending order
$array = $array | Sort-Object -Descending # Sorts $array in descending order
# Hashtable Operations
$hashtable["age"] # Access the value associated with the key 'age'
$hashtable["age"] = 26 # Modify the value for 'age'
$keys = $hashtable.Keys # Returns all keys
$values = $hashtable.Values # Returns all values
# File Operations
$file = "file.txt"
$content = Get-Content $file # Reads the entire file content
Set-Content $file -Value "New content" # Writes to a file
Add-Content $file -Value "Append content" # Appends to a file
# Error Handling
try {
Throw "Error"
}
catch {
Write-Output "Caught an error: $_"
}
finally {
Write-Output "This runs no matter what"
}
# Importing Modules
Import-Module Microsoft.Graph # Import a specific module
# Commonly Used Functions
$length = $array.Length # Returns the number of items in $array
$dataType = $x.GetType() # Returns the type of $x
$sum = $array | Measure-Object -Sum # Sums all items in $array
$max = $array | Measure-Object -Maximum # Returns the max value
$round = [math]::Round(3.14159, 2) # Rounds to 2 decimal places
# List Comprehensions (PowerShell doesn't support list comprehensions directly, but similar results can be achieved)
$squares = for ($i = 0; $i -lt 10; $i++) { $i * $i }
# Lambda Functions (ScriptBlock in PowerShell)
$increment = { param($x) $x + 1 }
$increment.Invoke(5) # This will return 6.
# Classes
class MyClass {
[string]$name
MyClass ([string]$name) {
$this.name = $name
}
[string] Greet() {
return "Hello, $($this.name)!"
}
}
$myObject = [MyClass]::new("Alice")
Write-Output $myObject.Greet() # Outputs: Hello, Alice!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment