Remove-Variable
Use Remove-Variable to delete a variable and its value from the session; -Force also clears read-only variables.
Documentation: Microsoft Learn — Remove-Variable
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Remove-Variable [-Name] <String[]> [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Scope <String>] [-WhatIf] [<CommonParameters>]- Stage 1Remove-Variable
-Name -Include -Exclude -Force -Scope -WhatIf
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Set-Variable -Name temp
Show reference output
Sample output · fictional data, shaped like the real thing
Get-Variable : Cannot find a variable with the name 'temp'. Check the name and try again.
At line:1 char:1
+ Get-Variable -Name temp
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (temp:String) [Get-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommandSet-Variable -Name locked
Show reference output
Sample output · fictional data, shaped like the real thing
Name Value ---- ----- ConfirmPreference High DebugPreference SilentlyContinue ErrorActionPreference Continue MaximumHistoryCount 4096
Parameters
Parameter definitions:
-Name-Include-Exclude-Force-Scope-WhatIf
Practical use and troubleshooting
Practical scenario: Use Remove-Variable when you need to delete a variable and its value from the session; -Force also clears read-only variables. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: A missing-variable error may be expected during repeated cleanup. Check the intended scope and remove only the binding that belongs to the current operation. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Get-Variable. Remove-Variable removes a variable binding; Get-Variable inspects it. Removing a name is different from assigning an empty string or null to its value. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Remove-Variable Use Remove-Variable to delete a variable and its value from the session; -Force also clears read-only variables.
Next: Get-Variable