Skip to content
neztach edited this page Aug 31, 2023 · 2 revisions
Code Explanation
$a = 0
Initialize a variable
[int] $a = 'Trevor'
Initialize a variable, with the specified type (throws an exception)
[string] $a = 'Trevor'
Initialize a variable, with the specified type (doesn't throw an exception)
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

Clone this wiki locally