Get-Command -Name *varia* # Get a list of commands related to variable management
Get-Variable # Get an array of objects, representing the variables in the current and parent scopes
Get-Variable | ? { $PSItem.Options -contains 'constant' } # Get variables with the "Constant" option set
Get-Variable | ? { $PSItem.Options -contains 'readonly' } # Get variables with the "ReadOnly" option set
New-Variable -Name FirstName -Value Trevor
New-Variable FirstName -Value Trevor -Option Constant # Create a constant variable, that can only be removed by restarting PowerShell
New-Variable FirstName -Value Trevor -Option ReadOnly # Create a variable that can only be removed by specifying the -Force parameter on Remove-Variable
Remove-Variable -Name firstname # Remove a variable, with the specified name
Remove-Variable -Name firstname -Force # Remove a variable, with the specified name, that has the "ReadOnly" option set
```
Operators
---------
```
$a = 2 # Basic variable assignment operator
$a += 1 # Incremental assignment operator
$a -= 1 # Decrement assignment operator
$a -eq 0 # Equality comparison operator
$a -ne 5 # Not-equal comparison operator
$a -gt 2 # Greater than comparison operator
$a -lt 3 # Less than comparison operator
$FirstName = 'Trevor'
$FirstName -like 'T*' # Perform string comparison using the -like operator, which supports the wildcard (*) character. Returns $true
$BaconIsYummy = $true
$FoodToEat = $BaconIsYummy ? 'bacon' : 'beets' # Sets the $FoodToEat variable to 'bacon' using the ternary operator
'Celery' -in @('Bacon', 'Sausage', 'Steak', 'Chicken') # Returns boolean value indicating if left-hand operand exists in right-hand array
'Celery' -notin @('Bacon', 'Sausage', 'Steak') # Returns $true, because Celery is not part of the right-hand list
5 -is [string] # Is the number 5 a string value? No. Returns $false.
5 -is [int32] # Is the number 5 a 32-bit integer? Yes. Returns $true.
5 -is [int64] # Is the number 5 a 64-bit integer? No. Returns $false.
'Trevor' -is [int64] # Is 'Trevor' a 64-bit integer? No. Returns $false.
'Trevor' -isnot [string] # Is 'Trevor' NOT a string? No. Returns $false.
'Trevor' -is [string] # Is 'Trevor' a string? Yes. Returns $true.
$true -is [bool] # Is $true a boolean value? Yes. Returns $true.
$false -is [bool] # Is $false a boolean value? Yes. Returns $true.
5 -is [bool] # Is the number 5 a boolean value? No. Returns $false.
```
Flow Control
------------
```
if (1 -eq 1) { } # Do something if 1 is equal to 1
do { 'hi' } while ($false) # Loop while a condition is true (always executes at least once)
while ($false) { 'hi' } # While loops are not guaranteed to run at least once
while ($true) { } # Do something indefinitely
while ($true) { if (1 -eq 1) { break } } # Break out of an infinite while loop conditionally
for ($i = 0; $i -le 10; $i++) { Write-Host $i } # Iterate using a for..loop
foreach ($item in (Get-Process)) { } # Iterate over items in an array
switch ('test') { 'test' { 'matched'; break } } # Use the switch statement to perform actions based on conditions. Returns string 'matched'
switch -regex (@('Trevor', 'Daniel', 'Bobby')) { # Use the switch statement with regular expressions to match inputs
'o' { $PSItem; break } # NOTE: $PSItem or $_ refers to the "current" item being matched in the array
}
switch -regex (@('Trevor', 'Daniel', 'Bobby')) { # Switch statement omitting the break statement. Inputs can be matched multiple times, in this scenario.
'e' { $PSItem }
'r' { $PSItem }
}
```
Functions
---------
```
function add ($a, $b) { $a + $b } # A basic PowerShell function
function Do-Something { # A PowerShell Advanced Function, with all three blocks declared: BEGIN, PROCESS, END
[CmdletBinding]()]
param ()
begin { }
process { }
end { }
}
```
Regular Expressions
-------------------
```
'Trevor' -match '^T\w*' # Perform a regular expression match against a string value. # Returns $true and populates $matches variable
$matches[0] # Returns 'Trevor', based on the above match
Write-Host -ForegroundColor Blue -Object 'Timer elapsed! Doing some work.'
}
$Timer.Start()
```
PowerShell Drives (PSDrives)
----------------------------
```
Get-PSDrive # List all the PSDrives on the system
New-PSDrive -Name videos -PSProvider Filesystem -Root x:\data\content\videos # Create a new PSDrive that points to a filesystem location
New-PSDrive -Name h -PSProvider FileSystem -Root '\\storage\h$\data' -Persist # Create a persistent mount on a drive letter, visible in Windows Explorer
Set-Location -Path videos: # Switch into PSDrive context
Remove-PSDrive -Name xyz # Delete a PSDrive
```
Data Management
---------------
```
Get-Process | Group-Object -Property Name # Group objects by property name
Get-Process | Sort-Object -Property Id # Sort objects by a given property name
Get-Process | Where-Object -FilterScript { $PSItem.Name -match '^c' } # Filter objects based on a property matching a value
gps | where Name -match '^c' # Abbreviated form of the previous statement
```
PowerShell Classes
------------------
```
class Person {
[string] $FirstName # Define a class property as a string