Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Clear-Variable

Updated: October 17, 2013

Applies To: Windows PowerShell 4.0

Clear-Variable

Deletes the value of a variable.

Aliases

The following abbreviations are aliases for this cmdlet:

  • clv

Syntax

Parameter Set: Default
Clear-Variable [-Name] <String[]> [-Exclude <String[]> ] [-Force] [-Include <String[]> ] [-PassThru] [-Scope <String> ] [-Confirm] [-WhatIf] [ <CommonParameters>]




Detailed Description

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

-Exclude<String[]>

Omits the specified items. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as "s*". Wildcards are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-Force

Allows the cmdlet to clear a variable even if it is read-only. Even using the Force parameter, the cmdlet cannot clear constants.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Include<String[]>

Clears only the specified items. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as "s*". Wildcards are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-Name<String[]>

Specifies the name of the variable to be cleared. Wildcards are permitted. This parameter is required, but the parameter name ("Name") is optional.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

true

-PassThru

Returns an object representing the cleared variable. By default, this cmdlet does not generate any output.


Aliases

none

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Scope<String>

Specifies 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 information, see about_Scopes.


Aliases

none

Required?

false

Position?

named

Default Value

Local

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • None

    You cannot pipe objects to Clear-Variable.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • None or System.Management.Automation.PSVariable

    When you use the PassThru parameter, Clear-Variable generates a System.Management.Automation.PSVariable object representing the cleared variable. Otherwise, this cmdlet does not generate any output.


Notes

  • To delete a variable, along with its value, use Remove-Variable or Remove-Item.

    Clear-Variable will 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.

Examples

-------------------------- EXAMPLE 1 --------------------------

This command deletes the value of global variables that have names that begin with "my".


PS C:\> Clear-Variable my* -Scope Global

-------------------------- EXAMPLE 2 --------------------------

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 a 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.


PS C:\> $a=3
PS C:\>&{ Clear-Variable a }
PS C:\>$a
3

-------------------------- EXAMPLE 3 --------------------------

This command deletes the value of the $processes variable. The $processes variable still exists, but the value is null.


PS C:\> Clear-variable -Name Processes

Related topics



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.