PowerShell One-Liner: Removing a System Variable

We’ve learned we can create system variables on a remote machine with the setx.exe command:

PSH> setx -m remote.machine My_System_Variable VariableData

However, setx doesn’t give us a way to remove the variable.

PSH> (new-object -com wscript.shell).environment(“system”).remove(“My_System_Variable”)

Why we have non-orthogonal commands like this is beyond me, but here at least is a way to work around it.

Yes, I know this isn’t really PowerShell – it’s more WSH (Windows Script Host) and COM scripting.  However, there are some things PowerShell does well, and they’re surprisingly limited.  For the rest, we have to rely on .NET, WMI, COM, and well-known command-line executables. 

Only they’re not so well-known.

Comments (0)