How to Remove Environment Variable in PowerShell
PowerShell provides multiple ways to remove an environment variable.
Method 1: Using the Remove-Item cmdlet with the Env: Drive
Remove-Item -Path Env:\my_variable
This script removes an environment variable my_variable from the current PowerShell session, as well as from the system environment variables.
Method 2: Using the [Environment] class to Remove Environment Variable
[Environment]::SetEnvironmentVariable("my_variable", $null, "Machine")
This script removes the environment variable my_variable permanently from the system environment variable.
Both these methods are used to delete environment variables in PowerShell.
The following example shows how to remove the environment variable.
Using the Remove-Item cmdlet to Remove Environment Variable
The Remove-Item cmdlet on the env: drive provider in PowerShell removes the environment variable from the current PowerShell session.
This example shows how to use it with syntax.
Remove-Item -Path Env:\RConstant
In this example, it deletes the RConstant environment variable.
If you try to get the environment variable using the following script, it will throw an exception as “it does not exist”.
Get-ChildItem -Path Env:\RConstant
Output:
(base) PS C:\> Get-ChildItem -Path Env:\RConstant
Get-ChildItem : Cannot find path 'RConstant' because it does not exist.
At line:1 char:1
+ Get-ChildItem -Path Env:\RConstant
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (RConstant:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
(base) PS C:\>
Using the [Environment] class to Remove Environment Variable
You can use the static method [SetEnvironmentVariable] of the [Environment] class to set an environment variable with a value of $null.
The following PowerShell script shows how to use it.
[Environment]::SetEnvironmentVariable("RConstant", $null, "Machine")
In this example, the “RConstant” environment variable will be removed from the machine-level environment variable by setting its value to $null.
Conclusion
I hope the above article on how to remove an environment variable in PowerShell using the Remove-Item and [Environment] class methods is useful to you.
Related Articles
Environment Variable Operations
- PowerShell Set Environment Variable - Create environment variables
- PowerShell Get Environment Variable - Retrieve environment variables
- PowerShell List Environment Variables - List all variables
- PowerShell Environment Hub - Complete environment management
Registry & System Configuration
- PowerShell Set Registry Value - Set registry values
- PowerShell Check If Registry Key Exists - Check registry keys
- PowerShell Get-ItemProperty - Retrieve item properties
- PowerShell Remove-Item - Remove files/registry entries
Path & Item Management
- PowerShell Test-Path - Test path existence
- PowerShell Get-Item - Get item objects
- PowerShell Get-ChildItem - List directory contents
- PowerShell Remove Item - Delete items
Data Processing & Filtering
- PowerShell Where-Object - Filter environment data
- PowerShell Select-Object - Select properties
- PowerShell ForEach-Object - Process items
- PowerShell Measure-Object - Calculate statistics
Control Flow & Error Handling
- PowerShell If-Else Statement - Conditional logic
- PowerShell Try-Catch - Error handling
- PowerShell Switch Statement - Switch logic
- PowerShell Error Handling - Comprehensive error handling
Variables & Storage
- PowerShell Variables - Variable management
- PowerShell Arrays - Work with collections
- PowerShell Hashtables - Store key-value pairs
- PowerShell Scope - Variable scope management
Output & Display
- PowerShell Output to File - Write to files
- PowerShell Write-Output - Display output
- PowerShell Format Table - Format display
- PowerShell Export CSV - Export data
Functions & Organization
- PowerShell Functions - Create reusable functions
- PowerShell Add-Member - Add custom properties
System Administration
- PowerShell Get-Process - Process information
- PowerShell Get-Service - Service information
- PowerShell Get-Environment - Environment overview
Data Types & Conversion
- PowerShell Data Types - Type handling
- PowerShell Null Handling - Null value operations
- PowerShell Strings - String operations
File & Batch Operations
- PowerShell List Files - List files
- PowerShell Import CSV - Bulk import
- PowerShell Batch Operations - Batch processing
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course
- Active Directory Guide - AD operations
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.