How to Set a Variable to Null in PowerShell?

One of my team members recently asked about setting a variable to null in PowerShell. There are different methods to do so. In this tutorial, I will show you how to set a variable to null in PowerShell.

To set a variable to null in PowerShell, you can simply assign $null to the variable using direct assignment. For example, $myVariable = $null will set the value of $myVariable to null, effectively clearing its contents.

Set a Variable to Null in PowerShell

In PowerShell, $null is an automatic variable used to represent NULL or an empty value. You can assign $null to variables, use it in comparisons, and use it as a placeholder for absent or undefined values in commands and scripts.

Now, let me show you different methods to set a variable to null in PowerShell with examples.

Method 1: Direct Assignment

The simplest way to set a variable to null in PowerShell is by directly assigning $null to the variable.

$myVariable = $null

After executing this command, $myVariable will have a null value.

Let me show you another example; you will learn how to set variables to null in a loop.

Consider a scenario where you are processing a list of user inputs and need to reset the variable after each iteration.

$inputs = @("Input1", "Input2", "Input3")

foreach ($input in $inputs) {
    $currentInput = $input
    # Process the input
    Write-Output "Processing $currentInput"
    
    # Reset the variable
    $currentInput = $null
}

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

Set a Variable to Null in PowerShell

Check out Check if a Variable is Null in PowerShell

Method 2: Clear-Variable Cmdlet

PowerShell provides a built-in cmdlet called Clear-Variable that allows you to set a variable to null.

$myVariable = "Some value"
Clear-Variable -Name myVariable

In this example, we first assign a value to $myVariable and then use the Clear-Variable cmdlet to set it to null.

Here is another example.

When you need to reuse a variable multiple times in a script, you can clear its value before assigning a new one.

$results = @("Result1", "Result2", "Result3")

foreach ($result in $results) {
    $currentResult = $result
    # Process the result
    Write-Output "Processing $currentResult"
    
    # Clear the variable for the next iteration
    Clear-Variable -Name currentResult
}

You can see the output in the screenshot below:

powershell set variable to null

Check out Share Variables Between Functions in PowerShell

Method 3: Remove-Variable Cmdlet

Another way to set a variable to null is by using the Remove-Variable cmdlet. This cmdlet removes the variable entirely from the current scope.

$myVariable = "Some value"
Remove-Variable -Name myVariable

After running these commands, $myVariable will no longer exist in the current scope.

Now, let me show you another real example to help you understand it better.

Suppose you have created several temporary variables in a script; you might want to remove them once they are no longer needed to free up memory.

Here is the script to set a variable to null.

$tempFilePath = "C:\Temp\file.txt"
# Perform operations using $tempFilePath

# Remove the temporary variable
Remove-Variable -Name tempFilePath

Write-Output "Temporary variable removed."

Check out PowerShell Print Variable

Method 4: Using [void] Type Accelerator

Another method to set a variable to null is by using the [void] type accelerator. This method is very useful when you want to explicitly discard a variable’s value.

[void]$myVariable

Now, let me show you another example.

Suppose you are running a command that produces output, but you are only interested in the side effects of the command and not its output.

# This command produces output, but we are not interested in it
[void](Get-Process)

# Proceed with other operations
Write-Output "Process information retrieved."

Conclusion

In this tutorial, I have shown you different methods to set a variable to null in PowerShell, including direct assignment, using the Clear-Variable cmdlet, and using the Remove-Variable cmdlet. I have also shown different examples of each method.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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