Reset Variables in PowerShell

While giving a webinar for a PowerShell user group in Chicago, someone asked me about resetting variables in PowerShell. There are different methods to do so. In this tutorial, I will show you how to reset variables in PowerShell with examples.

To reset a variable in PowerShell, you can use the Clear-Variable cmdlet, which removes the variable’s value without deleting the variable itself. For example, if you have a variable $UserName with a value of “JohnDoe”, you can reset it by executing Clear-Variable -Name UserName. After this command, $UserName will be set to $null, ensuring it no longer holds any previous data.

Reset Variables in PowerShell

There are various methods to reset variables in PowerShell, including different methods with examples.

1. Using Clear-Variable

The Clear-Variable cmdlet in PowerShell removes the value of a variable without deleting the variable itself. This means the variable remains in memory but is set to $null.

Example:

Here is an example to help you understand it better.

# Assign a value to the variable
$UserName = "JohnDoe"

# Clear the variable
Clear-Variable -Name UserName

# Check the value of the variable
if ($null -eq $UserName) {
    Write-Output "The variable is null."
} else {
    Write-Output "The variable is not null."
}

In this example, $UserName is cleared, ensuring it no longer holds the value “JohnDoe”. When executed correctly, this script should output “The variable is null.” indicating that the Clear-Variable cmdlet has successfully set $UserName to $null.

I executed the above PowerShell script using VS code. Look at the screenshot below for your reference.

Reset Variables in PowerShell

Check out How to Check if a Variable is Empty in PowerShell?

2. Set the Variable to $null

Another method to reset a variable in PowerShell is to set the variable to $null directly.

Let me show you an example.

Example:

# Assign a value to the variable
$FilePath = "C:\Temp\file.txt"

# Reset the variable
$FilePath = $null

# Check the value of the variable
$FilePath # Output: $null

This method is simple and effective, especially for resetting no longer needed variables.

Here is the output in the screenshot below after I executed the above PowerShell script.

reset variable in powershell

3. Using Remove-Variable

The Remove-Variable cmdlet in PowerShell deletes the variable entirely from the current session.

Example:

Here is an example.

# Assign a value to the variable
$SessionID = 12345

# Remove the variable
Remove-Variable -Name SessionID

# Check if the variable exists
$SessionID # Output: The term '$SessionID' is not recognized...

This method is useful to ensure that the variable is completely removed from the session.

Check out Check if a Variable Exists in PowerShell

4. Using New-Variable to Reinitialize

You can also use New-Variable to reinitialize a variable with a new value or as $null in PowerShell.

Example:

Here is an example of resetting a variable in PowerShell using the New-Variable cmdlet.

# Assign a value to the variable
$ServerName = "Server01"

# Reinitialize the variable
New-Variable -Name ServerName -Value $null -Force

# Check the value of the variable
$ServerName # Output: $null

This method is particularly useful when resetting and reinitializing a variable in one step.

Check out Escape Special Characters in Variables in PowerShell

Reset Variables in PowerShell Examples

Now, let me show you a few real examples of how to reset a variable in PowerShell.

Example 1: Reset User Input

Consider a script that collects user input for multiple operations. Resetting the input variable ensures that previous inputs don’t affect subsequent operations.

# Collect user input
$UserInput = Read-Host "Enter your username"

# Perform some operations
Write-Output "Processing $UserInput"

# Reset the variable for next input
$UserInput = $null

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

powershell reset variable

Example 2: Clear Temporary Data

In a script that processes files, you might want to clear the file path variable after each file is processed to avoid processing the same file multiple times.

# Process files in a directory
$Files = Get-ChildItem -Path "C:\Temp"

foreach ($File in $Files) {
    $FilePath = $File.FullName
    Write-Output "Processing $FilePath"
    
    # Process the file
    # ...

    # Clear the variable
    Clear-Variable -Name FilePath
}

Conclusion

I hope now you learn everything about different methods for resetting variables in PowerShell using different methods such as:

  1. Using Clear-Variable
  2. Set the Variable to $null
  3. Using Remove-Variable
  4. Using New-Variable to Reinitialize

And I hope the examples have helped you. Do let me know in the 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.