PowerShell variables
Variables in PowerShell are relatively straight forward. And if you want to use a variable in a string, you are free to do so:
$myVar = 3
Write-Host "MyVar: $myVar" # MyVar: 3
$myObject = [PSCustomObject]@{
Country = 'Portugal'
Capital = 'Lisbon'
}
Write-Host "Country: $myObject.Country" # Country: @{Country=Portugal; Capital=Lisbon}.Country
Write-Host "Country: $($myObject.Country)" # Country: Portugal
The interesting thing we see above is that if we have an object (or array) and you want to use a property in a string, you should use subexpression operator $( ). This way, you force the PowerShell to evaluate the complex expression first.
- Previous: PowerShell and environment variables
- Next: Freeing port 80 on Windows