Variables
- Declaration and assignment:powershell
$name = 'Alice' $count = 10 $price = 12.5 $null # Null value - Scope:
- Local/Script/Global/Private, commonly used for controlling variable visibility between functions and scripts
- Common automatic variables:
$_(current pipeline object),$PSVersionTable,$Args,$Error,$PID,$LastExitCode
- Read environment variables:
$env:Path; Set:$env:MyVar = 'value' - Interpolation and subexpressions:
- String interpolation:
"Hello $name" - Subexpressions:
"2+2=$([int](2+2))"
- String interpolation:
Type constraints and type casting:
powershell
[int]$count = '42' # Automatically converts to int
[datetime]$d = '2024-01-01'Tip: Avoid abusing variables in global scope; pass data between functions through parameters and return values to improve maintainability.