Clear-Variable
Clear-Variable [-Name*] <String[]> [-Confirm] [-Exclude <String[]>] [-Force] [-Include <String[]>] [-PassThru][-Scope <String>] [-WhatIf] [<CommonParameters>]
The Clear-Variable cmdlet deletes the data stored in a variable, but it does not delete the variable. As a result, the value of the variable is NULL (empty). If the variable has a specified data or object type, Clear-Variable preserves the type of the object stored in the variable.
Parameters
-Confirm [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Prompts you for confirmation before running the cmdlet.
-Exclude <String[]>
- Default value is None
- Accepts pipeline input False
Specifies an array of items that this cmdlet omits in the operation. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as "s*". Wildcards are permitted.
-Force [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Allows the cmdlet to clear a variable even if it is read-only. Even using the Force parameter, the cmdlet cannot clear constants.
-Include <String[]>
- Default value is None
- Accepts pipeline input False
Specifies an array of items that this cmdlet includes in the operation. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as "s*". Wildcards are permitted.
-Name <String[]>
- This value is required
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the name of the variable to be cleared. Wildcards are permitted. This parameter is required, but the parameter name ("Name") is optional.
-PassThru [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.
-Scope <String>
- Default value is None
- Accepts pipeline input False
Specifies the scope in which this alias is valid.
The acceptable values for this parameter are:
- Global
- Local
- Script
You can also use 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.
-WhatIf [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Shows what would happen if the cmdlet runs. The cmdlet is not run.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
NoneOutputs
None or System.Management.Automation.PSVariableExamples
-
Remove the value of global variables that begin with a search
string:
PS C:\> Clear-Variable my* -Scope Global
This command removes the value of global variables that have names that begin with my.
-
Clear a variable in a child scope but not the parent scope:
PS C:\> $a=3 PS C:\> &{ Clear-Variable a } PS C:\> $a 3
These commands demonstrate that clearing a variable in a child scope does not clear the value in the parent scope. The first command sets the value of the variable $A to 3. The second command uses the invoke operator (&) to run the Clear-Variable command in a new scope. The variable is cleared in the child scope (although it did not exist), but it is not cleared in the local scope. The third command, which gets the value of $A, shows that the value 3 is unaffected.
-
Delete the value of the specified variable:
PS C:\> Clear-variable -Name "Processes"
This command deletes the value of the variable named Processes. After the cmdlet completes the operation, the variable named Processes still exists, but the value is null.
Additional Notes
* To delete a variable, along with its value, use Remove-Variable or Remove-Item. This cmdlet does not delete the values of variables that are set as constants or owned by the system, even if you use the Force parameter. If the variable that you are clearing does not exist, the cmdlet has no effect. It does not create a variable with a null value. You can also refer to Clear-Variable by its built-in alias, clv. For more information, see about_Aliases. *
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands