How to Clear Variable Content in PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use the Clear-Variable Cmdlet to Clear Variable Content in PowerShell
  2. Use the Remove-Variable Cmdlet to Remove the Variable in PowerShell
  3. Conclusion
How to Clear Variable Content in PowerShell

In PowerShell, we can store all types of values in a variable. You can store the outputs of commands and the elements used in commands and expressions, such as names, paths, settings, and values.

Variables are represented by text strings and begin with the $ sign in PowerShell. They are not case-sensitive and can include spaces and special characters.

Sometimes, when working with variables in PowerShell, you might need to clear the values of variables. This tutorial will teach you to clear variable content in PowerShell.

Use the Clear-Variable Cmdlet to Clear Variable Content in PowerShell

The Clear-Variable cmdlet helps to remove the data stored in a variable. It deletes only the value, not a variable.

Clearing the value will assign the variable’s $null (empty) value. It preserves the data or object type stored in the variable.

The following command creates a variable named $var and assigns the string values.

$var = "Hello World"

Next, we will use Clear-Variable to remove the value from the variable $var.

Clear-Variable -Name var

In the command above, the -Name parameter tells the Clear-Variable cmdlet which variable you want to clear, and var is the specific variable name you’re targeting. Now, the var variable still exists after it completes the operation, but the value is null.

$var

Output:

It gives no output, which means there is an empty value in the variable.

To remove multiple variable values, you can specify multiple variable names.

Clear-Variable -Name var1 var2 var3

You can also use the command clv to delete the value of a variable. clv is the command alias of Clear-Variable.

clv -Name var

Use the Remove-Variable Cmdlet to Remove the Variable in PowerShell

In contrast, the Remove-Variable cmdlet deletes not only the content but the entire variable, rendering it inaccessible for further use in the current session. It is useful when you want to eliminate a variable.

This cmdlet does not delete variables set as constants or owned by the system.

The following command removes the variable var.

Remove-Variable var

When we execute the command above, it will delete the variable named var from the current session, making it unavailable for further use.

Alternatively, the rv command alias can be used for the same purpose.

rv var

Conclusion

Understanding how to work with variables in PowerShell is essential, and there are various methods for doing so. Whether you need to clear variable content while keeping the variable itself (Clear-Variable) or completely remove a variable (Remove-Variable), these cmdlets allow you to efficiently manipulate variables based on your specific needs, contributing to more effective PowerShell scripting and management.

We hope this article helped you understand how to clear the contents of the variables PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Variable