Remove the value from a variable. The value will become NULL (empty) but Clear-Variable will preserve the data-type of the object stored in the variable.

      Clear-Variable [-name] string[]
         [-include string[]] [-exclude string[] ]
            [-scope string] [-force] [-passThru] [-whatIf]
               [-confirm] [CommonParameters]
   -name string
       The name(s) of the variable to be cleared, required.
       Wildcards are permitted.

   -include string[]
       Clear only the specified items, wildcards allowed e.g. "ora*"

   -exclude string[]
       Omit the specified items, wildcards allowed e.g. "*ms*"

   -scope string
       The scope in which this alias is valid. 
       Valid values are "Global", "Local", or "Script", or a number relative
       to the current scope ( 0 through the number of scopes, where 0 is the
       current scope and 1 is its parent). "Local" is the default.
       For more, type "get-help about_scope".

   -force SwitchParameter
       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or override a files read-only attribute, but will not change file permissions. 
       Even using -Force, Clear-Variable cannot clear CONSTANTS.
       Pass the object created by Clear-Variable through the pipeline.
       (By default this switch is not set)

       Describe what would happen if you executed the command without actually
       executing the command.
       Prompt for confirmation before executing the command.

Standard Aliases for Clear-Variable: clv


Clear a local variable:

PS C:\> clear-variable -name ss64

Clear a global variable:

PS C:\> clear-variable ss64 -global

Clearing a variable in a child scope (e.g. via the invoke operator: &{....} ) will not clear the variables value in the parent scope.

“It is not the strongest species that survives nor the most intelligent but rather the one that is most adaptive to change” ~ Charles Darwin

Related PowerShell Cmdlets

Get-Variable - Get a PowerShell variable.
New-Variable - Create a new variable.
Remove-Variable - Remove a variable and its value.
Set-Variable - Set a variable.
Environment Variables
Equivalent bash commands: env - Display, set, or remove environment variables.

Copyright © 1999-2024
Some rights reserved