Updating the system path using Windows Script Host

On a recent business trip, I needed to add the full path to Microsoft Office and Lexis-Nexis CompareRite into the system path. (All workstations were running the French edition of Windows 2000.) Doing this manually - finding the installation path for the applications (since I wasn’t sure if all installations were to the same location on the local hard drives), then manually pasting or typing the paths into the System control panel - was too time-consuming for me, so I wrote a script to do it.

The Windows Script Host (WSH) engine is included with Windows 2000, so I wrote a WSH script using VBScript to do the necessary work:

  1. Read the current system path from the registry.
  2. Read the path to Microsoft Office from the registry.
  3. If the Office path is not already in the system path, append it.
  4. Read the path to CompareRite from the registry.
  5. If the CompareRite path is not already in the system path, append it.
  6. Update the system path in the registry if it changed.

The script follows:


'FILENAME: updpath.vbs

Option Explicit

‘Stop at all errors by defaultOn Error Goto 0

‘AddToPath: If sAdd is not in the string sPath, append sAdd to sPath and return the result;‘           otherwise, return sPath unchanged.Function AddToPath ( sAdd, sPath )  If InStr( 1, sPath, sAdd, 1 ) = 0 Then    AddToPath = sPath & ";" & sAdd    WScript.Echo sAdd & " added to path."  Else    AddToPath = sPath  End IfEnd Function

‘DoScript: The main script procedure.Sub DoScript  Dim WshShell, strPath, strOrigPath, strAppPath  Set WshShell = WScript.CreateObject("WScript.Shell")

  On Error Resume Next

  ‘Read the system path. Exit on errors.  strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")  If Err.Number <> 0 Then Exit Sub  strOrigPath = strPath

  ‘Read the Office path and call AddToPath.  strAppPath = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Office\8.0\BinDirPath")  If Err.Number = 0 Then     strPath = AddToPath(strAppPath, strPath)  End If

  ‘Read the CompareRite path and call AddToPath.  strAppPath = WshShell.RegRead("HKLM\SOFTWARE\LEXIS-NEXIS\Office\7.0\CompareRite\Directory")  If Err.Number = 0 Then     strPath = AddToPath(strAppPath, strPath)  End If

  ‘Update the system path if it’s changed.  If strPath <> strOrigPath Then    WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath, "REG_EXPAND_SZ"    If Err.Number <> 0 Then Exit Sub    WScript.Echo "System path updated to:" & vbCrLf & strPath  Else    WScript.Echo "Path not updated - no updates necessary."  End IfEnd Sub

Call DoScript()

There are two things to consider with this script. First, the changes do not take effect until the user logs off and logs back on (no reboot, just re-logon). Second, the script requires Administrator-level privileges, because the registry key where the system path is stored is read-only to non-administrators. Using the Windows 2000 runas command, we can run the script when logged in as a non-administrator user by running the following line from a command prompt. Just change the user parameter to an administrator-level account, and the path to the actual location of the updpath.vbs file.

runas /profile /user:domain\administrator "wscript.exe \\server\share\updpath.vbs"

Change wscript.exe to cscript.exe if you want the output to appear in a console (command prompt) window.

Bookmark and Share

Popularity: 13% [?]

Related Posts

Free market competition, unfettered by unnecessary governmental regulatory restraints, is the best way to foster innovation and development of the Internet. Free market competition drives scarce resources to their fullest and most efficient use, spurring businesses to invest in and sell as efficiently as possible the kinds and quality of goods and services that consumers desire. ast experience has demonstrated that, absent actual market failure, the operation of a free market is a far superior alternative to regulatory restraints.

-- Department of Justice

Post a Comment

Your email is never published nor shared. Required fields are marked *