PowerShell Delete Variable | Remove All Variables From Memory In Powershell

When writing PowerShell scripts, it’s important to clean up after yourself by properly removing variables that are no longer needed. This helps free up memory, prevents unintended variable reuse, and keeps your script environment tidy. In this tutorial, I will explain different methods to delete variables in PowerShell with examples.

In the same tutorial, I will also explain how to remove all variables from memory in PowerShell using different methods.

To delete a variable in PowerShell, you can use the Remove-Variable cmdlet. This cmdlet removes the specified variable from the current session, effectively deleting it. For example, to delete a variable named $UserName, you would use the command Remove-Variable -Name UserName.

Delete Variables in PowerShell

Now, let me show you different methods to clear variables in PowerShell.

1. Remove-Variable Cmdlet

The Remove-Variable cmdlet is the primary method to delete variables in PowerShell. It removes the specified variable from the current session.

Syntax:

Remove-Variable -Name <VariableName> [-Scope <Scope>] [-Force] [-ErrorAction <ActionPreference>] [-ErrorVariable <VariableName>] [-OutVariable <VariableName>] [-OutBuffer <Int32>] [<CommonParameters>]

Example:

$userCount = 100
$userNames = @("John", "Alice", "Bob")

# Perform some operations with the variables

Remove-Variable -Name userCount, userNames

In this example, we define two variables: $userCount and $userNames. After using these variables in our script, we use the Remove-Variable cmdlet to delete them, specifying their names with the -Name parameter.

I executed the above PowerShell script, and you can see the output in the screenshot below:

PowerShell Delete Variable

Check out PowerShell Print Variable

2. Clear-Variable Cmdlet

The Clear-Variable cmdlet in PowerShell clears the value of a variable but does not remove the variable itself. This can be useful when resetting a variable without deleting it.

Syntax:

Clear-Variable -Name <VariableName> [-Scope <Scope>] [-Force] [-ErrorAction <ActionPreference>] [-ErrorVariable <VariableName>] [-OutVariable <VariableName>] [-OutBuffer <Int32>] [<CommonParameters>]

Example:

$salesData = @{
    "New York" = 1000000
    "Los Angeles" = 850000
    "Chicago" = 700000
}

# Process the sales data

Clear-Variable -Name salesData

In this scenario, we have a $salesData variable containing sales information for different cities in the USA. After processing the data, we use Clear-Variable to clear the value of $salesData, effectively resetting it to an empty variable.

You can see the output in the screenshot below:

powershell clear all variables

3. Using Remove-Variable with Wildcards

The next method will help you to clear multiple variables in PowerShell. You can use wildcards with Remove-Variable to delete multiple variables that match a pattern.

Example:

$User1 = "JohnDoe"
$User2 = "JaneDoe"
Remove-Variable -Name User*

This script will remove both $User1 and $User2 variables.

Check How to Increment A Variable in PowerShell?

4. Deleting Variables in a Specific Scope

Variables can exist in different scopes, such as global, script, or local. You can specify the scope when deleting a variable.

Example:

$global:UserName = "JohnDoe"
Remove-Variable -Name UserName -Scope Global

This script removes the $UserName variable from the global scope.

Here is another real-time example.

Suppose you want to clean up all user-defined variables at the end of a script to ensure they do not interfere with subsequent scripts. Then you can write the script like below:

$UserName = "JohnDoe"
$UserEmail = "john.doe@example.com"
# Perform operations with $UserName and $UserEmail
Get-Variable -Scope Local | ForEach-Object { Remove-Variable -Name $_.Name }

This script removes all variables in the local scope in PowerShell.

Learn How to Get the Type of a Variable in PowerShell?

Remove All Variables From Memory In Powershell

Now, let us see how to remove all variables from memory in PowerShell using different methods.

1. Using Get-Variable with Remove-Variable

One of the best way to clear all variables is by combining Get-Variable with Remove-Variable. This method retrieves all variables and then removes them.

Example:

Get-Variable | Remove-Variable -ErrorAction SilentlyContinue

In this script, Get-Variable retrieves all the variables, and Remove-Variable deletes them. The -ErrorAction SilentlyContinue parameter ensures that no errors are displayed if a variable cannot be removed.

2. Excluding Essential Variables

Sometimes, you may want to exclude certain essential variables from being cleared, such as those related to the PowerShell environment or preferences.

Example:

Get-Variable -Exclude PWD,*Preference,PS* | Remove-Variable -ErrorAction SilentlyContinue

This script excludes variables like PWD, any variable ending with Preference, and all variables starting with PS (which include important PowerShell variables).

3. Clearing Variables in Specific Scopes

PowerShell variables can exist in different scopes (global, script, local). You can target a specific scope when clearing all variables.

Example:

Get-Variable -Scope Global | Remove-Variable -Scope Global -ErrorAction SilentlyContinue

This script clears all global variables. You can change the scope to Script or Local as needed.

Check out PowerShell Variable Naming Conventions

4. Using Wildcards with Remove-Variable

You can use wildcards with the Remove-Variable cmdlet to remove multiple variables that match a specific pattern in PowerShell. This is useful when you have a group of related variables.

Example:

# Define some variables with a common prefix
$Server01_Name = "Server01"
$Server02_Name = "Server02"
$Server03_Name = "Server03"

# Remove all variables that start with 'Server'
Remove-Variable -Name 'Server*'

In this example, all variables that start with Server are removed using a wildcard pattern.

5. Clearing All Variables in the Session

To remove all variables in the current PowerShell session, you can use the Remove-Variable cmdlet with the * wildcard. This approach is useful for cleaning up the session entirely.

Example:

# Remove all variables in the current session
Remove-Variable -Name * -ErrorAction SilentlyContinue

Using -ErrorAction SilentlyContinue suppresses any errors that might occur if some variables cannot be removed.

Conclusion

Removing variables in PowerShell is essential for writing clean, efficient, and secure scripts. In this tutorial, I explained different methods to delete variables in PowerShell, such as the Remove-Variable Cmdlet and Clear-Variable Cmdlet.

Still have some questions? Feel free to leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.