• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1396
  • Last Modified:

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

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
ucstechinfo
Asked:
ucstechinfo
  • 11
  • 11
1 Solution
 
yehudahaCommented:
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
 
yehudahaCommented:
and the destinatiom path here

strFavoriteRedirectLocation = "\\dc\Redirected Folders\%username%\Favorites"
0
 
yehudahaCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
yehudahaCommented:
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
 
yehudahaCommented:
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
 
yehudahaCommented:
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
 
ucstechinfoAuthor Commented:
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
 
yehudahaCommented:
so write in the path h:

:-)

write what ever you want
0
 
ucstechinfoAuthor Commented:
I get a script error at line 11 character 18 about syntax.

The registry path does not change.
0
 
yehudahaCommented:
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
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
yehudahaCommented:
you asked for my documents and thats what you got

basicly if you want redirection of other folders it can be done
0
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
ucstechinfoAuthor Commented:
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
 
yehudahaCommented:
you got the idea
glad i  helpd

don't forget to close the question.
0
 
ucstechinfoAuthor Commented:
I'm still having trouble with the if statements
0
 
yehudahaCommented:
ucstechinfo you asked for a redirection script to my documents folder

if you want to write another script please post another question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now