Jan 28 2008

How to redirect users’ favorites folder using VBScript

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Searchmarked.com Tip

Update: wnaquin in the comments below pointed out that the users’ actual favorites were not in the new location. Keep in mind that this script does not actually copy any data to the new location. So this will work for new users, but for existing users you will have to copy their data manually or through a logon script. The below .bat or .cmd file should work:

@echo off
REM # The below line creates the directory structure.
xcopy "%userprofile%\favorites" "\\Server\share\%username%\favorites" /i /t /q /y
REM # The below line copies the files.
xcopy "%userprofile%\favorites" "\\Server\share\%username%\favorites" /i /q /y

Every Windows System administrator knows what a pain user profiles can be. This is especially true if they are roaming profiles. That is why I have made it my personal mission to rid the world of roaming profiles. I have enabled folder redirection at work through Group Policy. This works for the “Application Data”, “My Documents”, and “Desktop” folders.

However, in Microsoft’s infinite wisdom, they decided to leave out the one folder that will make our users scream the loudest. The “Favorites” folder. There is no setting in Group Policy for the “Favorites” folder. So I had to script it. Below are the results in VBScript.

'==========================================================================
'
' NAME: fav_redirect.vbs
'
' AUTHOR: Harley Stagner
' DATE : 1/24/2008
'
' COMMENT: This script will redirect users folders to the File server
' yourfileserver
'==========================================================================
'**************************************************************************
'********************************Header Section*****************************
'**************************************************************************
Option Explicit
'Variable for shell object
Dim wshShell
'Variable for redirecting Favorites
Dim strFavoriteRedirectRegKey
Dim strFavoriteRedirectLocation
Dim strFavoriteRedirectRegType
'**************************************************************************
'********************************Reference Section**************************
'**************************************************************************
'CREATE GLOBAL OBJECTS
Set wshShell = CreateObject("WScript.Shell")
' INITIALIZE VARIABLES----------------------------------------------------------------------------------------------
'Favorites Redirection Registry Info
strFavoriteRedirectRegKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites"
strFavoriteRedirectLocation = "\\YOURFILESERVER\Redirected Folders\%username%\Favorites"
strFavoriteRedirectRegType = "REG_EXPAND_SZ"
'**************************************************************************
'**********************************Worker/Output Section********************
'**************************************************************************
'Redirect Favorites by writing registry entry.
wshShell.RegWrite strFavoriteRedirectRegKey, strFavoriteRedirectLocation, strFavoriteRedirectRegType

Just copy and paste the code into notepad and save the file with a *.vbs extension and you’re ready to rock. You will need to replace some information in the script with details that are specific to your environment (like the redirection location). Also keep in mind that the initialization of the variables are one variable per line. For example, where it says “strFavoriteRedirectRegKey= Some value” that statement should be on one line. Also the statement:

wshShell.RegWrite strFavoriteRedirectRegKey, strFavoriteRedirectLocation, strFavoriteRedirectRegType

Should be on one line. The formatting of the web page throws this off sometimes.

You can either run the script locally (to test its functionality) or you can set this as a login script for users via Active Directory Group Policy. That’s it! Happy folder redirecting!

Technorati Tags: , , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

16 Comments on this post

Trackbacks

  1. Chris said:

    Do you have a VB script to redirect the Outlook to the users home directory. This needs to include the nk2 file (cache addresses) and multi Outlook profiles going to an Exchange Server. Don’t want Group Policies as only needed for 6 users. Fairly extensive AD in use here.

    February 21st, 2008 at 12:58 am
  2. hstagner said:

    Hello Chris,

    All that you need to do is replace any reference in this script to “Favorites” with “Application Data”.

    If you go to the registry key listed in the script, you will notice that “Application Data” is also listed there. Since “Application Data” contains your outlook profiles and nk2 files, that should be all you need. I hope this helps.

    Thank you for reading!

    Regards,

    Harley Stagner

    February 21st, 2008 at 3:07 pm
  3. Chris said:

    Thankyou very much Harley! You gave me a great start to my epic script.
    I’ve added extra registry redirection settings (Desktop, My Documents, Local Settings\application data, Application Data) , printer mappings and a registry import for Outlook settings although a logoff script within Group Policies is required to export reg settings to a reg file in the users home directory. A pause command was used for the favorites as when a new Windows profile is created on a domain PC, the script ran before the new Favorites folder was created hence wiping previously saved favorites on the home directory. I’m happy to show you the script if you like.
    Thanks again for your help. Chris.

    February 27th, 2008 at 5:28 am
  4. Alex H said:

    I keep getting an error (Invalid Character) when I run this script which is coming from this line — Set wshShell = CreateObject(“WScript.Shell”).

    April 24th, 2008 at 4:37 pm
  5. Jose said:

    Alex,

    the invalid characters are the double quotes, they got mangled by the web and the copy/paste, just retype them

    May 18th, 2008 at 2:46 pm
  6. Moses Bwana said:

    I am trying to get a script that will enable me ;

    1) Create roaming profile mapped on say H: for all users with their names
    2) Redirecting the my documents folder to (server)H:
    3) Mapping network drivers say L: to a shared folder

    Please help.

    Thanks

    Moses

    June 5th, 2008 at 1:58 am
  7. hstagner said:

    Hello Moses,

    First, thank you for reading!

    Are you using Active Directory?

    (1) If so then the roaming profile is assigned to the user account by specifying the url (e.g. \\server\share\profiles\%username%). I am not sure why you would want to map a drive for the roaming profile.

    (2) You can redirect my documents to the server by Group Policy in active directory- If you are not using group policy then you can use the script above and replace references to “Favorites” with “My Documents”. Again, I am not sure why you need a drive mapping for this (it’s done by URL and the user just accesses My Documents as normal).

    (3) Just mapping a drive is easily accomplished in a .bat or .cmd file. The command would be:

    net use L: \\server\share

    Please let me know if you have any other questions.

    Also, If you end up redirecting “My Documents”, “Desktop”, “Application Data” and “Favorites” then there really is no need for a roaming profile other than printer mapping which can be done via scripting.

    Regards,

    Harley Stagner

    June 5th, 2008 at 8:24 am
  8. anil said:

    Nice script. But just wonder when I create a fresh user with the option of system creates the profile , then profile folder is empty until the user logs off. So in those conditions writing a registry path which is invalid until the user logs off, so this script behaves?

    June 25th, 2008 at 12:09 pm
  9. anil said:

    Just wonder do you advise me how to configure Folders for New users. I mean if user folders exists, simply I can make registry changes using those folders. But new users logs in with a default profile , then how should I approach. One way I can think is pre create the folder and do the registry changes, is there any other way?

    June 25th, 2008 at 3:01 pm
  10. hstagner said:

    Hello Anil,

    Creating the folder ahead of time with the appropriate permissions would be your best bet. Also, if the script is running as a log-on script you should be good to go as the registry change will happen when the user logs on.

    June 27th, 2008 at 12:57 am
  11. wnaquin said:

    I keep getting an error (Invalid Character) when I run this script which is coming from line 15, character 1.

    July 1st, 2008 at 5:45 pm
  12. hstagner said:

    Hello wnaquin,

    The problem you are experiencing is the copy/paste from the website. Take a look at the apostrophe in the beginning of line 15. It translates as a tick instead of an apostrophe when you do the copy/paste. Just change all those ticks into apostrophe’s and you should be good to go.

    Thank you for reading! If you want to stay updated on the posts at this site, consider subscribing to the RSS feed.

    July 1st, 2008 at 10:02 pm
  13. wnaquin said:

    Thanks for the help. I have the script up an running. I tested it on a small user group and their favorites are no longer showing up in ie and they did not redirect the their home folfer. Here is what I pasted in the RedirectionLocation:

    strFavoriteRedirectLocation = “\\NameofServer\Users\%username%\Favorites”

    Did I do something wrong?

    July 2nd, 2008 at 11:53 am
  14. hstagner said:

    Hello wnaquin,

    You did not do anything wrong. I left out an important note. You actually have to manually copy the contents of existing users’ Favorites folders into the new location (or you could script it with a simple batch or cmd file at logon). Something like the following would work:

    @echo off

    REM # The below line creates the directory structure.

    xcopy “%userprofile%\favorites” “\\Server\share\%username%\favorites” /i /t /q /y

    REM # The below line copies the files.

    xcopy “%userprofile%\favorites” “\\Server\share\%username%\favorites” /i /q /y

    I hope this helps. Thanks for reading! I will update the post with this information up top. If you would like to stay updated on my blog posts, consider subscribing to my RSS feed.

    July 2nd, 2008 at 1:00 pm
  15. PMABKE said:

    What about a laptop user…The redirected folders will not be available when they are off the network. Is this were Offline files comes in to play?

    April 9th, 2009 at 10:35 am
  16. sb1340 said:

    The script on this page works great. Is there a line that be put into it that check to see if the favorites is already on the users network drive, that it would not create a new favorites folder? Rather it would keep the path pointing only to the users network drive for the favorites that are already there. Right now the script re-creates a favorites folder overwriteing the existing one

    May 27th, 2011 at 3:21 am

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                  Computers Blogs - Blog Top Sites