Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: how to delete a path in the Path environment variable

Tags:

powershell

I used "setx" to add a new path to my PATH environment variable. How do I see whether we can delete the newly added path from PATH environment variable?

like image 803
derek Avatar asked Aug 18 '16 05:08

derek


People also ask

How do I delete a path in PowerShell?

In the PowerShell console, type Remove-Item –path c:\testfolder –recurse and press Enter, replacing c:\testfolder with the full path to the folder you want to delete.

Can I delete PATH environment variable?

If you select a variable and press Edit, you can delete the value, but you cannot press OK, as this button gets grayed out. Therefore you cannot save your changes. However, you can clear the value of an environment variable using Command Prompt.

How do I delete a path variable?

We can comment or remove the last line from the /etc/profile. d/jdk. sh script to remove Java's path from the $PATH variable permanently. After removing the path from the file, when a user starts a new session, it won't have that directory in the $PATH variable.

How do I remove something from Windows path?

Removing Directories from the PATH Variable It's easiest to simply open the GUI, copy the contents of the PATH variable (either the User Path or the System Path) to a text editor, and remove the entries you want to delete. Then paste the remaining text back into the Edit Path window, and save.


1 Answers

Deleting a specific value from %PATH% needs you to get the variable, modify it, and put it back.

For example.

# Get it
$path = [System.Environment]::GetEnvironmentVariable(
    'PATH',
    'Machine'
)
# Remove unwanted elements
$path = ($path.Split(';') | Where-Object { $_ -ne 'ValueToRemove' }) -join ';'
# Set it
[System.Environment]::SetEnvironmentVariable(
    'PATH',
    $path,
    'Machine'
)
like image 188
Chris Dent Avatar answered Oct 17 '22 18:10

Chris Dent