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 = $nullAfter 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:

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 myVariableIn 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:

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 myVariableAfter 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]$myVariableNow, 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:
- PowerShell Global Variables
- Concatenate String and Variable in PowerShell
- Create Variables with Multiple Values in PowerShell
- Check if a Variable Exists in PowerShell
- Escape Special Characters in Variables in PowerShell
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.