Guy’s Scripting Ezine 114 – Prune the Environmental Path

Guy’s Scripting Ezine 114 – Prune the Environmental Path

 ♣

This Week’s Secret

I just love it when readers send me their scripts.  The most satisfying scripts are those where I give a reader a little help and then they go away and solve the problem themselves.  This week I feature such a script kindly sent in by Xihui Zhang.

This Week’s Mission Amending the Path Environmental Variable

This Week’s Mission is to remove an individual element from the Environmental Path.  If you remember, the path statement enables you to type just the name of the executable and not worry about the drive letter and folder.  Now if there is one thing worse than no path values, it’s having an invalid path statement.  The real life situation that Xihui faced was to prune the path on 1000 machines, hence it was worth creating a job VBScript, which automates the task.

To investigate the background to the script, either type PATH at the cmd prompt or else visit the System Icon, Advanced, Environmental Variables.  Here is an example of the path that you may see: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Support Tools\;C:\Program Files\Windows PowerShell\v1.0\;

I rarely issue health warnings about my scripts, partly because if the code goes wrong the worst case scenario is – nothing happens.  The other reason is nobody ever reads long disclaimers.  However, this week I anticipate that if the script goes wrong then it could wipe out your path statement, which could be irritating rather than terminal.  Therefore I suggest you take precautions and save your path to a text file, at the cmd prompt type:

path > guypath.txt    This will save your path into notepad. 

If the following script makes a mistake and deletes your path, you could copy the text from the saved file and paste the path into the Environmental Variables tab of the System Icon.  If you have to resort to this technique then remember to remove ‘Path=’ from the front of the text string.  My hidden agenda is persuading you to look at the path before the script prunes one of the elements.

Pre-requisites

This script is designed to work on any Windows machine.  Nevertheless, before your script can remove a path string, you have to give it a chance and make sure that the value for strSearchFor is in your path.  Therefore, to prepare for the script, I recommend that you launch the System Icon, Advanced, Environmental Variables and search the Computer variables (not the User Variables) for the path statement.  Now add the same value as you set in strSearchFor.  I chose strSearchFor = "C:\log; ", either use my value, else amend the script if you choose a different  value for strSearchFor.

Example : Prune the Path Statement

Instructions for adding multiple groups to one user

  1. Copy and paste the example script below into notepad or use a VBScript editor. E.g. OnScript.

  2. Save the file with a .vbs extension, for example: Path.vbs 

  3. Double click Path.vbs, then check at the command prompt, type Path.  Observe that your script has removed the value referenced by strSearchFor.

Updated Version 3.8

‘ ===============================================
‘ Path.VBS
‘ To remove strings in PATH ENVIRONMENT VARIABLE
‘ Authors Xihui Zhang and Guy Thomas
‘ Version 3.8 – June 2005
‘ ==============================================
Option Explicit
On Error Resume Next

Dim objReg, strComputer, strValueRight, strValueLeft
Dim strSearchFor, strKeyPath, strEntryName, strValue
Dim strTotalLength, strSearchForLength, strStartAt
dim strArraySearchFor

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
‘ Each value in the Array must end with a semicolon ";"
strArraySearchFor=Array("c:\log;", "c:\logs;")

‘ WMI Section which connects to registry (StdRegProv)
‘ Gets value for strValue (GetExpandedStringValue)
Set objReg=GetObject _
("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" _
& strComputer & "\root\default:StdRegProv")

strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strEntryName = "path"
objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
strEntryName,strValue

If (right(strValue, 1) <> ";") then
strValue=strValue+";"
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strEntryName, strValue
end if

for each strSearchFor in strArraySearchFor
surgery
Next

Wscript.Quit(1)

‘ End of Path VBScript

‘ ——————————————————–

Function surgery
‘ Calculates strStartAt which cuts the strSearchFor value from the path
strSearchForLength=len(strSearchFor)
strStartAt = inStr(lCase(strValue), lCase(strSearchFor))

‘ Builds the new Path
‘ Joins the Values to the left and right of strSearchFor
strTotalLength=len(strValue)
strValueLeft=left(strValue,(strStartAt-1))
strValueRight=right(strValue, _
(strtotalLength-strSearchForLength-(strStartAt-1)))
strValue=strValueLeft + strValueRight

‘ Reset the Path in the registry (SetStringValue)
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strEntryName, strValue
end function

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Original Version

 

================================
‘  Path.VBS
‘  To remove a string in PATH ENVIRONMENT VARIABLE
‘  Authors Xihui Zhang and Guy Thomas
‘  Version 3.2 – May 2005
‘  ==============================================
Option Explicit
Dim objReg, strComputer, strValueRight, strValueLeft
Dim strSearchFor, strKeyPath, strEntryName, strValue
Dim strTotalLength, strSearchForLength, strStartAt, strAdd

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
‘  The value in variable strSearchFor must end with a semicolon ";"
strSearchFor="e:\log;"

‘  WMI Section which connects to registry (StdRegProv)
‘  Gets value for strValue (GetExpandedStringValue)
Set objReg=GetObject _
("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" _
& strComputer & "\root\default:StdRegProv")

strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strEntryName = "path"
objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
strEntryName,strValue

‘  ——————————————————–
‘  Calculates strStartAt which cuts the strSearchFor value from the path
strSearchForLength=len(strSearchFor)
strStartAt = inStr(lCase(strValue), lCase(strSearchFor))

if strStartAt=0 then
     strValue=strValue+";"
     strStartAt = inStr(lCase(strValue), lCase(strSearchFor))
     if strStartAt=0 then
         Wscript.quit(0)
     end if
end if

‘  Builds the new Path
‘  Joins the Values to the left and right of strSearchFor
strTotalLength=len(strValue)
strValueLeft=left(strValue,(strStartAt-1))
strValueRight=right(strValue, _
(strtotalLength-strSearchForLength-(strStartAt-1)))
strValue=strValueLeft + strValueRight

‘  Reset the Path in the registry (SetStringValue)
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strEntryName, strValue

Wscript.Quit(1)

‘  End of Path VBScript

Learning Points

Note 1: ("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" _
& strComputer & "\root\default:StdRegProv").   You may have seen scripts that employ LDAP:// RootDSE to access Active Directory, well here WMI connects to the registry with StdRegProv.

Note 2: The Standard Registry Provider (StdRegProv) requires us to set Const HKEY_LOCAL_MACHINE.  I tried removing this CONST value, but the script failed with a nasty error, after that I just accepted the need for the Const statement.

Note 3: As luck would have it, Yitzchok Lavi wrote explaining that winmgmts:{impersonationLevel=impersonate works better if you add:   ,(Security).

Note 4:  Its well worth tracking strValue to observe how the script manipulates the value that the registry holds for Path.

Note 5:  One of the star features of this script is the way that it slices up the long path statement, removes the value specified by strSearchFor, then rejoins the two remaining pieces of the path to remake the new statement minus the offending value.

Note 6:  For me the last pieces of understanding how this script worked were examining GetStringValue and SetStringValue.

See more Windows 8 registry examples

Summary of Environmental Path

At face value this script just removes an offending value from the path statement, for example C:\log.  Yet, if you delve deeper you will discover a wealth of techniques that you can apply to other scripts, for example, StdRegProv with its corresponding CONST value.

See More WMI Scripts

• WMI  • Ezines  • WMI Basics   • WMI PowerShell  • Free SolarWinds WMI Monitor

Ezine 9 WMI  • Ezine 10 WMI  • Ezine 19 WMI  • Ezine 40 Variables  • Ezine 48 WMI 

Ezine 52 WMI OS  • Ezine 76 WMI Classes  • Ezine 93 WMI  • Ezine 94 WMI  • Ezine 95 WMI

Ezine 110 WMI PowerShell  •Ezine 114 WMI Path  • Tool Kit