Home » PowerShell » PowerShell Clear-Variable

PowerShell Clear-Variable

The Clear-Variable cmdlet in PowerShell clear variable value, as a result, the variable value is empty or NULL.

At times, it is important to clear or unset variables to free up memory or avoid unintended consequences in your PowerShell scripts.

Clearing variable value removes data in the variable and makes it empty. It preserves the type of object stored in the variable if the PowerShell variable has a specified object of data type.

In this article, we will discuss how to clear variables in PowerShell using the Clear-Variable cmdlet.

Clear-Variable Cmdlet in PowerShell

The Clear-Variable cmdlet deletes the value of the variable. To clear a single variable, use the following syntax:

#Define the variable

$temp = "Hello World"

# Get the Type of the variable
$temp.GetType()

# Clear variable value
Clear-Variable -Name "temp"

# Print the variable to check value
$temp

# Check if the variable is null
$temp -eq $null

In the above PowerShell script, the $temp variable stores the string type data value. The Clear-Variable cmdlet uses the -Name parameter to specify the variable name and delete the content of the variable. If we print the variable $temp, it displays empty. Finally, we check if the $temp is equal to $null and it returns the True value.

The output of the above script clears the variable value in PowerShell.

PowerShell Clear Variable Value
PowerShell Clear Variable Value

Assign $null to Variables to Clear Variable Value

To clear a variable value, assign the $null value to it. As a result, the value of a variable is NULL.

# Define the variable
$temp = "Hello ShellGeek!" 

# Assign the #null value to variable                                                                                      $temp = $null

# Print the variable to check its value                                                                                                    $temp

# Check if the variable is null                                                                                                      $temp -eq $null  

In the above PowerShell script, the $temp variable stores the string data value in it. By assigning the $null value to the $temp variable, it clears a variable value.

The output of the above script is:

PS D:\> $temp = "Hello ShellGeek!"                                                                                      PS D:\> $temp = $null                                                                                                   
PS D:\> $temp                                                                                                           

PS D:\> $temp -eq $null                                                                                                 True

PS D:\>     

Clear All Variables in PowerShell Session

To clear all variables in a PowerShell session, use the Get-Variable cmdlet to get all the variables defined in the session and the Clear-Variable cmdlet to clear all variable’s values.

Get-Variable | ForEach-Object { Clear-Variable -Name $_.Name }

In the above PowerShell script, the Get-Variable cmdlets retrieve all the variables in a PowerShell session and use the pipe operator (|) to pass them through the ForEach-Object to iterate them and use Clear-Variable to clear all variables stored data.

PowerShell Unset Variable using Remove-Variable cmdlet

Use the Remove-Variable cmdlet to unset a variable entirely.

# Define the variable
$temp = "Hello ShellGeek!" 

# Remove-Variable to unset variable                                                                                  Remove-Variable -Name "temp"

Conclusion

I hope the above article on how to clear variables in PowerShell using the Clear-Variable or Remove-Variable cmdlet is helpful to you.

By using Clear-Variable, or assigning a $null value to a variable, you can clear the variable data and manage your scripts effectively. Be cautious when clearing variables like system variables or variables required for the proper execution of your script.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.