On occasion, you might want to use read-only or constant variables in your code. This recipe will show you how to create variables with specific options, such as read-only or constant, and why you would use those. You will also see some important automatic variables that are, incidentally, also constants.
Read-only and constant variables
How to do it...
Install and start PowerShell Core and execute the following steps:
- Try to execute the following code block:
$true = $false
$pid = 0
$Error = "I don't make mistakes!"
- Execute the next cmdlet to see why Step 1 didn't work as intended:
Get-Variable | Where-Object -Property Options -like *Constant*
- You can create your own read-only and constant variables...