Recently, someone asked me about clearing variables in PowerShell. There are different methods to do so. In this tutorial, I have explained how to clear variables in PowerShell using different methods with examples.
To clear a variable in PowerShell, you can use the Clear-Variable cmdlet, which deletes the data stored in a variable without removing the variable itself. The syntax is straightforward: Clear-Variable -Name <VariableName>. For example, if you have a variable $employeeName set to “John Doe”, executing Clear-Variable -Name employeeName will clear its value, setting it to $null.
Methods to Clear Variables in PowerShell
1. Using Clear-Variable Cmdlet
The Clear-Variable cmdlet deletes the data stored in a variable but does not remove the variable itself. This results in the variable being set to $null.
This is one of the recommended ways to clear a variable in PowerShell. Let me show you the syntax and an example.
Syntax:
Clear-Variable -Name <VariableName>Example:
$employeeName = "John Doe"
$employeeID = 12345
# Clearing the variables
Clear-Variable -Name employeeName
Clear-Variable -Name employeeID
# Output the variables to see the result
Write-Output $employeeName # Output:
Write-Output $employeeID # Output: You can see the output in the screenshot below after I executed the above PowerShell script.

Check out PowerShell Pipeline Variables
2. Assigning $null
Assigning $null to a variable is a straightforward way to clear its value in PowerShell. Here is the syntax and an example.
Syntax:
$<VariableName> = $nullExample:
$projectName = "Migration Project"
$projectDeadline = "2024-12-31"
# Clearing the variables
$projectName = $null
$projectDeadline = $null
# Output the variables to see the result
Write-Output $projectName # Output:
Write-Output $projectDeadline # Output: Here is the output in the screenshot below:

Read PowerShell Private Variables
3. Using Remove-Variable Cmdlet
The Remove-Variable cmdlet in PowerShell deletes the variable entirely, freeing up the memory it occupies.
Syntax:
Remove-Variable -Name <VariableName>Example:
$serverName = "Server01"
$serverIP = "192.168.1.1"
# Removing the variables
Remove-Variable -Name serverName
Remove-Variable -Name serverIP
# Trying to output the variables will result in an error
# Write-Output $serverName # Error: The variable '$serverName' cannot be found.
# Write-Output $serverIP # Error: The variable '$serverIP' cannot be found.Check out PowerShell Print Variable
4. Using Get-Variable with a Wildcard
You can use Get-Variable with a wildcard character to clear multiple variables at once in PowerShell.
Syntax:
Get-Variable -Name "<WildcardPattern>" | Clear-VariableExample:
$task1 = "Documentation"
$task2 = "Testing"
$task3 = "Deployment"
# Clearing all variables starting with 'task'
Get-Variable -Name "task*" | Clear-Variable
# Output the variables to see the result
Write-Output $task1 # Output:
Write-Output $task2 # Output:
Write-Output $task3 # Output: You will get the exact output in the screenshot below when you execute the PowerShell script above.

5. Using Remove-Variable with a Wildcard
Similar to Clear-Variable, you can use Remove-Variable with a wildcard to delete multiple variables in PowerShell.
Let me show you the syntax and the complete example.
Syntax:
Remove-Variable -Name "<WildcardPattern>"Example:
$devServer = "DevServer01"
$testServer = "TestServer01"
$prodServer = "ProdServer01"
# Removing all variables ending with 'Server'
Remove-Variable -Name "*Server"
# Trying to output the variables will result in an error
# Write-Output $devServer # Error: The variable '$devServer' cannot be found.
# Write-Output $testServer # Error: The variable '$testServer' cannot be found.
# Write-Output $prodServer # Error: The variable '$prodServer' cannot be found.Conclusion
In this tutorial, I have explained how to clear variables in PowerShell using different methods such as Clear-Variable Cmdlet, assigning $null, Remove-Variable Cmdlet, and Get-Variable with a Wildcard, etc. I have also explained different examples related to clearing variables in PowerShell.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.