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.

Popularity: 11% [?]

Related posts:

  1. Renaming files in a subdirectory tree
  2. Separating SQL script files generated by Microsoft SQL (by type)
  3. Stop the “restart” popup from Windows Update
  4. SalesLogix command line switches not working?
  5. Forgot your Windows product key? It is recoverable…

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>