PowerShell Genie

Remove-Variable

Use Remove-Variable to delete a variable and its value from the session; -Force also clears read-only variables.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Remove-Variable

Syntax

1PS> Remove-Variable [-Name] <String[]> [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Scope <String>] [-WhatIf] [<CommonParameters>]
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>]
  1. Stage 1Remove-Variable-Name -Include -Exclude -Force -Scope -WhatIf

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Set-Variable -Name temp

    1PS> Set-Variable -Name temp -Value 'value'; Remove-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.GetVariableCommand

    Set-Variable -Name locked

    1PS> Set-Variable -Name locked -Value 1 -Option ReadOnly; Remove-Variable -Name locked -Force
    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

    Remove-Variable on Microsoft Learn

    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.

    Remove-Variable reference on Microsoft Learn

    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
    Genie guideRemove-Variable

    Use Remove-Variable to delete a variable and its value from the session; -Force also clears read-only variables. The console below is pre-loaded — run it, then change a value and run it again.