I am trying to prepopulate a user's home directory before they log in for the first time. I would like to do this programmically. Right now, I can create a the folder, but when a user logs in, it creates a different one with the domain added to the end. Here is what so far.

@echo off

:: Create user folder on C:\
mkdir "C:\USERS\%1"
:: copy folder structure from template to new user folder
mkdir "c:\Users\%1\Desktop"
mkdir "c:\Users\%1\Documents"
mkdir "c:\Users\%1\Downloads"
mkdir "c:\Users\%1\Music"
mkdir "c:\Users\%1\Pictures"

:: Set rights on new folder
::: Remove inheritance, grant owner everything and admin everything
icacls "C:\USERS\%1" /inheritance:r /grant DOMAIN\%1:(OI)(CI)F
icacls "C:\USERS\%1" /grant %computername%\Administrator:(OI)(CI)F

echo Folder creation complete


However, the folder wont be the user's home directory. I am guessing that I will need to add a registry key with the user's GUID to HKLM > SOFTWARE > Microsoft > Windows NT > CurrentVersion > ProfileList, but unsure how to get the GUID from the username, and if I need to do anything else.

Note: I don't want do change the user's home directory for every computer, just the one that they are logging in.

  • Why not just do it at first logon instead of ahead of time? – GregL Mar 7 '17 at 14:39
  • @GregL These are VMs being automatically created per request by a user. It is just a preference that everything is completed while a machine is being provisioned. I don't have any experience at doing things at first login. I'll try searching for more information on that. If you have anything I can start from, it would be appreciated. – Loaf Mar 7 '17 at 15:20
  • 1
    Windows will create the folder for the user when the user first logs on. Let Windows do it's job and get out of it's way. There's no advantage to creating the folder beforehand and as you've seen, it's creating a problem for you, for which you've create a cumbersome workaround. – joeqwerty Mar 7 '17 at 16:36
  • @joeqwerty Are you saying the answer I posted is a cumbersome workaround, and I should go about this another way? Or it would be cumbersome to work around how I was previously going about it. – Loaf Mar 7 '17 at 16:40
  • 1
    I am unable to edit the properties of their user account. However, their home directory is located in the correct place anyways once they log in. I am also not trying to redirect folders either. I copied code from another script I had so some comments may be misleading. – Loaf Mar 7 '17 at 18:47

What I did to resolve the issue was add a registry key at HKLM > SOFTWARE > Microsoft > Windows > Current Version > Run that ran a script in another location. The scripts main purpose was to create a folder in the users home directory and move a file. If the folder already existed, I just exited the script.

I originally had it in RunOnce, however this would only run for the first any user logged in, and wouldn't run if a different user logged in for the first time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.