Bloated Path Variable in Windows

25 September 2016

After running a Windows OS for a while and installing some software, the path variable is often filled up with entries you don't really need, run the following line in PowerShell:

($env:path -split ";") | ? {if (Test-Path ([System.Environment]::ExpandEnvironmentVariables("$_"))){Write-Host $_}else{Write-Host $_ -f red}}

It shows all the entries in %path%, on my current system it looks like this:

C:\WINDOWS\system32 C:\WINDOWS C:\WINDOWS\System32\Wbem C:\WINDOWS\System32\WindowsPowerShell\v1.0\ Q:\bin Q:\sbin C:\Program Files\Microsoft SQL Server\110\Tools\Binn\ C:\Program Files\Microsoft SQL Server\110\DTS\Binn\ C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\ C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\ C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\ C:\WINDOWS\System32\Windows System Resource Manager\bin C:\WINDOWS\idmu\common C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.9\ C:\Program Files (x86)\Windows Live\Shared C:\Program Files\TortoiseGit\bin C:\Program Files (x86)\Skype\Phone\ C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static C:\Program Files\Microsoft\Web Platform Installer\ C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\ C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\ C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\ %USERPROFILE%\.dnx\bin C:\Program Files\Microsoft DNX\Dnvm\ C:\Program Files\Microsoft SQL Server\120\Tools\Binn\ C:\Program Files\Microsoft SQL Server\130\Tools\Binn\ C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\ C:\Program Files (x86)\Microsoft Emulator Manager\1.0\ C:\Users\username\AppData\Local\Microsoft\WindowsApps

WTF, and I'm sure there are worse cases around. Especially SQL Server seems to feel it is very important.

It's not really a big problem to have too many entries in the %path% variable, but I like to keep things lean.

Plus every time I execute a command without a fully qualified path, Windows will look in all these locations to find it. If that program doesn't exist on my computer it takes longer to tell me if there are many location to be searched. Okay, this may only be a few milliseconds, but still.

The red entry above %USERPROFILE%\.dnx\bin means that path doesn't actually exist on my computer.

To clean things up, I decided to just remove most entries and see what happens. First I made a backup, we have to be careful because the way the path variable is presented to the user is not the way it is stored in the registry. This is because the value:

HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name\Path

is of type REG_EXPAND_SZ, meaning a string that has any variables in it expanded first and then returned. So %SystemRoot% becomes C:\Windows. You could use regedit.exe to export the whole Environment key, but it stores the path value in an ugly hex-encoded value.

I use this PowerShell code:

($(New-Object -com "WScript.Shell").Environment("System"))["Path"] | Set-Content -Path .\PathVar.txt

Now I removed everything after the first six entries and rebooted my machine.

C:\WINDOWS\system32 C:\WINDOWS C:\WINDOWS\System32\Wbem C:\WINDOWS\System32\WindowsPowerShell\v1.0\ Q:\bin Q:\sbin C:\Users\username\AppData\Local\Microsoft\WindowsApps

The one thing I knew would break is using sqlcmd.exe without the full path. While looking into this I learned that SQL Server 2016 doesn't even come with sqlcmd.exe, not even the SQL Management tools which you have to download separately come with it. You have to download it separately.

Anyways, one way to deal with programs that may expect to be in the path, is to use intermediate batch files, like: git.cmd

X:\PortableGit\cmd\git.exe %1 %2 %3 %4 %5 %6 %7 %8 

or sqlcmd.cmd:

"%ProgramFiles%\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\SQLCMD.EXE" %1 %2 %3 %4 %5 %6 %7 %8 

there are in my Q:\bin directory so I can use git or sqlcmd anywhere and they just redirect the call to the correct location.

Now, over time I will see what else will break and how to fix it which may involve putting certain entries back into the path Variable.

I just don't see the point of having an entry in there for a program that I may use once a year and that I could start just as well using its full path.

Tags: Windows

Pages in this section

Categories

ASP.Net | Community | Development | IIS | IT Pro | Security | SQL (Server) | Tools | Web | Work on the road | Windows