PowerShell Variables

$a = 0Initialize 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-VariableGet 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 ConstantCreate a constant variable, that can only be removed by restarting PowerShell
New-Variable FirstName -Value Trevor -Option ReadOnlyCreate a variable that can only be removed by specifying the -Force parameter on Remove-Variable
Remove-Variable -Name firstnameRemove a variable, with the specified name
Remove-Variable -Name firstname -ForceRemove a variable, with the specified name, that has the “ReadOnly” option set

Leave a Reply

Your email address will not be published. Required fields are marked *