?
Solved

How do I create a VBS script to change a registry key for all users in the domain?

Posted on 2009-02-15
22
Medium Priority
?
1,355 Views
Last Modified: 2012-05-06
We have user's My Documents folder remapping to a UNC path which is also the same path for their Home Directories which is mounted as a drive letter at login.

We are moving storage locations, and need to change the My Documents redirection path on the local machine from the \\SERVERNAME\SHARENAME to H:\

Using a GPO Folder Redirection policy does not work.

I want to find all user accounts in the HKU hive, including the default, and change the value, write a log file so the script can check to see if a computer has been fixed and exit.  I also want to have it pop up a notification window during this process so the user knows they will be logged off when the script completes and they can then log in again.

I've been trying with regular batch files and with
@ECHO OFF
IF EXIST C:\temp GOTO next
:next
IF EXIST c:\temp\%USERNAME%.log GOTO end
MKDIR C:\temp
echo %date% %time% > C:\temp\%USERNAME%.log
c:\windows\system32\reg.exe delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal" /f >> c:\temp\%USERNAME%.log 2>&1
c:\windows\system32\reg.exe add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal /t REG_SZ /d "H:\\" /f >> c:\temp\%USERNAME%.log 2>&1
logoff
:end
exit
 
 
and this one I found via google
 
REM Modify a registry key in for all logged in users
REM Also modify it in the .DEFAULT hive so future users get it.
REM
REM Copyright Jared Barneck
REM
 
GOTO main
 
:modkey
  REM %1 is the value of %a that is passed.
  REG Add HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders /v Personal /t REG_SZ /d H:\\
  REM Going to :end here only ends this instance of the call to the 
  REM :modkey label. It does not end the whole batch file.
GOTO end
 
:main
  FOR /F "tokens=2* delims=\" %%a IN ('REG QUERY HKU ^|Findstr /R "DEFAULT S-1-5-[0-9]*-[0-9-]*$"') DO CALL :modkey %%a
  REM Going to :end here ends the whole batch file.
GOTO end
 
:end

Open in new window

0
Comment
Question by:ucstechinfo
  • 11
  • 11
22 Comments
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646163
change the message in this line and the seconds as you wish

wshshell.Popup "Hello in one minute the system will logg off",60

here :


Set wshShell = CreateObject("WScript.Shell")
strFavoriteRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strFavoriteRedirectLocation = "\\dc\Redirected Folders\%username%\Favorites"
strFavoriteRedirectRegType = "REG_EXPAND_SZ"
wshShell.RegWrite strFavoriteRedirectRegKey, strFavoriteRedirectLocation, strFavoriteRedirectRegType
wshshell.Popup "Hello in one minute the system will logg off",60
wshshell.Run("shutdown -l")

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646170
and the destinatiom path here

strFavoriteRedirectLocation = "\\dc\Redirected Folders\%username%\Favorites"
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646178
fix
Set wshShell = CreateObject("WScript.Shell")
strRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation = "\\dc\Redirected Folders\%username%\documents"
strRedirectRegType = "REG_EXPAND_SZ"
wshShell.RegWrite strRedirectRegKey, strRedirectLocation, strRedirectRegType
wshshell.Popup "Hello in one minute the system will logg off",60
wshshell.Run("shutdown -l")

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 14

Expert Comment

by:yehudaha
ID: 23646216
crap

in my clock it's 10 pm after a hard working day :-)

Set wshShell = CreateObject("WScript.Shell")
Set objfso = CreateObject("scripting.filesystemobject")
Set objnet = CreateObject("wscript.network")
strcomputer = objnet.ComputerName
Set objlog = objfso.CreateTextFile("\\server\share\log.txt")
strRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation = "\\dc\Redirected Folders\%username%\documents"
strRedirectRegType = "REG_EXPAND_SZ"
On Error Resume Next
wshShell.RegWrite strRedirectRegKey, strRedirectLocation, strRedirectRegType
If Err.Number <> Then
   objlog.WriteLine "error writing to registry on " & strcomputer & vbcrlf
Else
    On Error Goto 0
    objlog.WriteLine "chnaged path on " & strcomputer & vbCrLf   
End if
wshshell.Popup "Hello in one minute the system will logg off",60
wshshell.Run("shutdown -l")

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646225
log path here:
Set objlog = objfso.CreateTextFile("\\server\share\log.txt")

message to users and time in seconds here:
Set objlog = objfso.CreateTextFile("\\server\share\log.txt")

redirect location here:
strRedirectLocation = "\\dc\Redirected Folders\%username%\documents"


0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646251
i'm so sorry but let's say you do it as a login script
maybe you need the user name to put in the redirect path
so i added struser variable that will hold user name

it can be:

strRedirectLocation = "\\dc\Redirected Folders\" & struser & "\documents"

or

strRedirectLocation = "\\dc\Redirected Folders\" & struser

%username% will not work
Set wshShell = CreateObject("WScript.Shell")
Set objfso = CreateObject("scripting.filesystemobject")
Set objnet = CreateObject("wscript.network")
struser = objnet.UserName
strcomputer = objnet.ComputerName
Set objlog = objfso.CreateTextFile("\\server\share\log.txt")
strRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation = "\\dc\Redirected Folders\%username%\documents"
strRedirectRegType = "REG_EXPAND_SZ"
On Error Resume Next
wshShell.RegWrite strRedirectRegKey, strRedirectLocation, strRedirectRegType
If Err.Number <> Then
   objlog.WriteLine "error writing to registry on " & strcomputer & vbcrlf
Else
    On Error Goto 0
    objlog.WriteLine "chnaged path on " & strcomputer & vbCrLf   
End if
wshshell.Popup "Hello in one minute the system will logg off",60
wshshell.Run("shutdown -l")

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646297
I can't set it to a UNC path, because we have two different storage locations now, which will eventually become three separate UNC paths depending on your UID.

It has to change the My Documents shell path to H: which is the user home directory path.

It was all fine the way we were doing it with one storage server.

We map:
H: user home directory
I: dept drive based on OU
J: inter-departmental drive
K: universal drive everyone gets mapped
P: user public_html which is a subfolder with different ACL's of their home directory

All these mount points are Samba servers mounted from SAN storage.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646330
so write in the path h:

:-)

write what ever you want
0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646374
I get a script error at line 11 character 18 about syntax.

The registry path does not change.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646444
i forgot a 0

fix
Set wshShell = CreateObject("WScript.Shell")
Set objfso = CreateObject("scripting.filesystemobject")
Set objnet = CreateObject("wscript.network")
struser = objnet.UserName
strcomputer = objnet.ComputerName
Set objlog = objfso.CreateTextFile("\\server\share\log.txt")
strRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation = "\\dc\Redirected Folders\%username%\documents"
strRedirectRegType = "REG_EXPAND_SZ"
On Error Resume Next
wshShell.RegWrite strRedirectRegKey, strRedirectLocation, strRedirectRegType
If Err.Number <> 0 Then
   objlog.WriteLine "error writing to registry on " & strcomputer & vbcrlf
Else
    On Error Goto 0
    objlog.WriteLine "chnaged path on " & strcomputer & vbCrLf   
End if
wshshell.Popup "Hello in one minute the system will logg off",60
wshshell.Run("shutdown -l")

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646454
Is it possible to do multiple strings?  Like Personal, Favorits, My Music, My Pictures are all subkeys of:

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\

The registry path is actually "User Shell Folders" and when I export the registry key, its all in Hex.
0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646459
Also, I found an older script done by my predecessors that enumerated the location for IE favorites, but was done in UNC format instead of drive letter with no explanations as to why.


' The script writes to the registry to change the default location to a user home
' The key Method is RegWrite.  RegLocate is the name of the variable 
' Note: REG_EXPAND_SZ
' Note: h:\favorites
 
Dim WSHShell, RegLocate, RegLocate1
Set WSHShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
RegLocate = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
 
' WSHShell.RegWrite RegLocate,"h:\favorites","REG_EXPAND_SZ"
 
WSHShell.RegWrite RegLocate,"\\server\share\favorites","REG_EXPAND_SZ"
 
WScript.Quit ' Tells the script to stop and exit.
 

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646471
you asked for my documents and thats what you got

basicly if you want redirection of other folders it can be done
0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646494
Would I just add more variables per path needing changed?

Example below
strRedirectRegKey1 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation1 = "H:\"
strRedirectRegType1 = "REG_EXPAND_SZ"
strRedirectRegKey2 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
strRedirectLocation2 = "H:\Favorites"
strRedirectRegType2 = "REG_EXPAND_SZ"
On Error Resume Next
wshShell.RegWrite strRedirectRegKey1, strRedirectLocation1, strRedirectRegType1, strRedirectRegKey2, strRedirectLocation2, strRedirectRegType2

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646574
This is my script based on your help,

The log file indicates an error writing to registry, but it appears everything else works.
Set wshShell = CreateObject("WScript.Shell")
Set objfso = CreateObject("scripting.filesystemobject")
Set objnet = CreateObject("wscript.network")
struser = objnet.UserName
strcomputer = objnet.ComputerName
Set objlog = objfso.CreateTextFile("\\blake\log\reglog.txt")
strRedirectRegKey1 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
strRedirectLocation1 = "H:\"
strRedirectRegType1 = "REG_EXPAND_SZ"
strRedirectRegKey2 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
strRedirectLocation2 = "H:\woufavs"
strRedirectRegType2 = "REG_EXPAND_SZ"
strRedirectRegKey3 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures"
strRedirectLocation3 = "H:\My Pictures"
strRedirectRegType3 = "REG_EXPAND_SZ"
strRedirectRegKey4 = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music"
strRedirectLocation4 = "H:\My Music"
strRedirectRegType4 = "REG_EXPAND_SZ"
On Error Resume Next
wshShell.RegWrite strRedirectRegKey1, strRedirectLocation1, strRedirectRegType1, strRedirectRegKey2, strRedirectLocation2, strRedirectRegType2, strRedirectRegKey3, strRedirectLocation3, strRedirectRegType3, strRedirectRegKey4, strRedirectLocation4, strRedirectRegType4
If Err.Number <> 0 Then
   objlog.WriteLine "error writing to registry on " & strcomputer & vbcrlf
Else
    On Error Goto 0
    objlog.WriteLine "changed path on " & strcomputer & vbCrLf   
End if
wshshell.Popup "Changing My Documents Storage Path, System Logoff In 1 Minute",60
wshshell.Run("shutdown -l")

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646659
This script appears to work as desired, just a few more things to add, like checking to see if the path has been updated per location, and then bypassing the changes and exiting if the paths have been changed.

Also, having the system log off at the end with the popup message warning the user of the changes so they don't call us.


' The script writes to the registry to change the default location to a user home
' The key Method is RegWrite.  RegLocate is the name of the variable 
' Note: REG_EXPAND_SZ
' Note: h:\favorites
 
Dim WSHShell, RegLocate, RegLocate1, RegLocate2, RegLocate3
Set WSHShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
RegLocate = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
RegLocate1 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\woufavs"
RegLocate2 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures"
RegLocate3 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music"
WSHShell.RegWrite RegLocate1,"h:\","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate1,"h:\woufavs","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate1,"h:\My Pictures","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate1,"h:\My Music","REG_EXPAND_SZ"
 
WScript.Quit ' Tells the script to stop and exit.

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646679
This is my working script, I want to check to see if the path has changed before executing, and have it exit so I can incorporate the popup message that something is changing and the system will log the user off.

I am at a loss as to how to execute the check for the path changes.


' The script writes to the registry to change the default location to a user home
' The key Method is RegWrite.  RegLocate is the name of the variable 
' Note: REG_EXPAND_SZ
' Note: h:\favorites
 
Dim WSHShell, RegLocate, RegLocate1, RegLocate2, RegLocate3, RegLocate4, RegLocate5, RegLocate6, RegLocate7
Set WSHShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
RegLocate = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
RegLocate1 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
RegLocate2 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures"
RegLocate3 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music"
RegLocate4 = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
RegLocate5 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Favorites"
RegLocate6 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Pictures"
RegLocate7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Music"
WSHShell.RegWrite RegLocate,"h:\","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate1,"h:\woufavs","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate2,"h:\My Pictures","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate3,"h:\My Music","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate4,"h:\","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate5,"h:\woufavs","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate6,"h:\My Pictures","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate7,"h:\My Music","REG_EXPAND_SZ"
wshshell.Popup "Changing My Documents Storage Path, System Logoff In 1 Minute",60
wshshell.Run("shutdown -l")
WScript.Quit ' Tells the script to stop and exit.

Open in new window

0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646688
I want to incorporate this type of code:

strRegistryValue = WshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")
WScript.Echo strRegistryValue

And have the script exit if it finds the values are = to the values I want to set in the script, and continue if they are not =
0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23646689
I think I've got it all sorted out, I've included the script
' The script writes to the registry to change the default location to a user home
' The key Method is RegWrite.  RegLocate is the name of the variable 
' Note: REG_EXPAND_SZ
' Note: h:\favorites
 
Dim WSHShell, RegLocate, RegLocate1, RegLocate2, RegLocate3, RegLocate4, RegLocate5, RegLocate6, RegLocate7
Set WSHShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
strRegistryValue = WshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")
WScript.Echo strRegistryValue 'Needs to enumerate the value of the key, and if it is h:\ proceed to check another key
if strRegistryValue = "h:\" else if 'needs to continue checking keys
strRegistryValue = WshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal")
WScript.Echo strRegistryValue 'Needs to enumerate the value of the key, and if it is h:\ it needs to quit at this point
if strRegistryValue = "h:\" end if 'Go to WScript.Quit
else
RegLocate = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"
RegLocate1 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
RegLocate2 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Pictures"
RegLocate3 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\My Music"
RegLocate4 = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal"
RegLocate5 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Favorites"
RegLocate6 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Pictures"
RegLocate7 = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Music"
 
WSHShell.RegWrite RegLocate,"h:\","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate1,"h:\woufavs","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate2,"h:\My Pictures","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate3,"h:\My Music","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate4,"h:\","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate5,"h:\woufavs","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate6,"h:\My Pictures","REG_EXPAND_SZ"
WSHShell.RegWrite RegLocate7,"h:\My Music","REG_EXPAND_SZ"
wshshell.Popup "Changing My Documents Storage Path, System Logoff In 1 Minute",60
wshshell.Run("shutdown -l")
WScript.Quit ' Tells the script to stop and exit.

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23646980
you got the idea
glad i  helpd

don't forget to close the question.
0
 
LVL 1

Author Comment

by:ucstechinfo
ID: 23647025
I'm still having trouble with the if statements
0
 
LVL 14

Accepted Solution

by:
yehudaha earned 2000 total points
ID: 23647244
ucstechinfo you asked for a redirection script to my documents folder

if you want to write another script please post another question
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question