Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm writing a Powershell script to do a complete Windows 7 Build. I have a function that adds the users I need, but it does not set the Home Directory, which makes a later section of my script (setting the users' desktops) fail because the directories "don't exist" yet.

Here is the main part of the function:

  [string]$computerName = $env:ComputerName, 
  [string]$description = "Created by PowerShell" 
 $computer = [ADSI]"WinNT://$computerName" 
 $user = $computer.Create("User", $userName) 
} #end function New-LocalUser 

Is there somewhere in there to add something akin to $user.put("HomePath","\Users\automotive\Desktop") for example so that it doesn't break the later part of my script.

Here is the code I'm using in my Users.cmd script:

Here's the code in my .cmd file:


SET Profile=C:\Users
SET user=cars
SET password=cars

SET PSEXEC="C:\Program Files\SysinternalsSuite\PsExec.exe" 
SET wmic=%windir%\System32\Wbem\WMIC

FOR /F "tokens=2* delims= " %%I IN ('REG QUERY ^"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList^" /v ProfilesDirectory^|find /i ^"ProfilesDirectory^"') DO (SET ProfilesDirectory=%%J) 
@echo ProfilesDirectory: %ProfilesDirectory% 

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%Profile%" /f
REG ADD "HKCU\Software\Systinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f

NET USER /add %user% %password% /comment:"Account for User" /expires:never /homedir:%Profile%\%user% 
NET LOCALGROUP Administrators /add %user%
%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%ProfilesDirectory%" /f

I call it with this command, followed by my commands to create the new Desktop.

\Windows\System32\cmd /c c:\Users\Administrator\Desktop\Users.cmd -wait

Remove-Item \Users\cars\Desktop -recurse -force
Copy-Item E:\"Desktop Cars" C:\Users\cars Desktop -recurse

The DOS commands run by fine until it gets to

%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"

That is followed by all of this in red before it gets to the final REG ADD command -

C:\>"C:\Program Files\SysinternalsSuite\PsExec.exe"  -h -u cars -p cars "C:\Windows\System32\Wbem\WMIC QUIT" 
cmd.exe : 
At C:\Users\Administrator\Desktop\Desktops.ps1:3 char:22
+ \Windows\System32\cmd <<<<  /c c:\Users\Administrator\Desktop\Users.cmd -wait
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

The system cannot find the file specified.

Connecting to local system...Starting PsExec service on local system...Connecting with PsExec service on ccm2756...Starting C:\Windows\
System32\Wbem\WMIC QUIT on ccm2756...
PsExec could not start C:\Windows\System32\Wbem\WMIC QUIT on ccm2756:

After that, it has added the account and the commands to change the Desktop work, but the files for the User's Desktop are "in use" until I reboot the system.

share|improve this question
Their user/desktop folder won't come into existence until they log in for the first time. –  Ƭᴇcʜιᴇ007 Aug 12 '13 at 17:34
Exactly techie007 - I'm looking for a way to bring those folders into existence using Powershell (the build is all in one script). This is one of the few snags I've run into so far. I'm thinking it may be related to the "\Users\Default" directory that already exists on the system, but I'm not sure how to use that nor implement it with Powershell. –  BigRedEO Aug 13 '13 at 18:30

1 Answer 1

Add user and set Home Directory at local:

NET USER UserName DerParol /add /comment:"Account for User" /expires:never /homedir:C\Acc\UserName

Add user at local Administrators group.

NET LOCALGROUP Administrators /add UserName

PSexec part of Sysinternals Suite

Create User Profile,



SET Profile=C:\Acc
SET user=Troll
SET password=DerParol

SET PSEXEC="C:\Program Files\SysinternalsSuite\PSEXEC"
SET wmic=%windir%\system32\wbem\wmic

FOR /F "tokens=2* delims= " %%I IN ('REG QUERY ^"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList^" /v ProfilesDirectory^|find /i ^"ProfilesDirectory^"') DO (
SET ProfilesDirectory=%%J)
@echo ProfilesDirectory: %ProfilesDirectory%

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%Profile%" /f
REG ADD "HKCU\Software\Sysinternals\PsExec" /v EulaAccepted /t REG_DWORD /d 1 /f

NET USER /add %user% %password% /comment:"Account for User" /expires:never /homedir:%Profile%\%user% /profilepath:%Profile%\%user%
NET LOCALGROUP Administrators /add %user%
%PSEXEC% -h -u %user% -p %password% "%wmic% QUIT"

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory /t REG_SZ /d "%ProfilesDirectory%" /f

Test <code>net user</code> command

Domain, use: dsadd user

@SET user=UserName
@SET password=DerParole

@SET pwdSet=-canchpwd yes -pwdneverexpires yes
::-mustchpwd yes

@SET accSet=-disabled no -acctexpires never -upn %user%@%USERDNSDOMAIN% -samid %user%
@SET dirSet=-profile \\SERV\Acc\%user% -hmdrv R: -hmdir \\SERV\Acc\%user% -loscr enver.cmd
@SET Brand=-office "Grand-1" -company TT -dept HT -title MegaUser
@SET locSet=-tel 914-55-04-42 -email %user%II@%USERDNSDOMAIN% -webpg %user%.%USERDNSDOMAIN% 
@SET callIn=-hometel 9-slip-010-292 -pager 1022 -mobile "8(777) 101-44-22" -fax "977-101-44-22" -iptel 6002
@SET nameSt=-fn User -ln Name -mi "U.N." -display "UserName II"
@SET Description=-desc "Account for User"

::@SET cmSet=@ECHO dsadd user
@SET cmSet=dsadd user

@For /F %%a In ('dsquery * -filter "(objectCategory=domain)"') Do @SET dc=%%a

@SET dn="cn=%user%,cn=users,%dc:~1%
::@SET grpSet=-memberof "cn=ResourceGroup,cn=users,%dc:~1%

%cmSet% -pwd %password% %dn% %pwdSet% %accSet% %dirSet% %Brand% %locSet% %callIn% %nameSt% %Description% -uc

dsquery *  -filter "(samAccountName=%user%)" -attr * | more

dsadd user test batch script:

dsadd user test batch script

Bulk Import CSVDE.

See: Step-by-Step Guide to Bulk Import and Export to Active Directory

share|improve this answer
There are a couple of problems with the NET USER /add command, STTR. It adds it as a Standard account and I need to add it as an Administrator account and (at least so far) I haven't found an option for adding it as such. Secondly, I've tested the NET USER /add command and then run a "md \Users\username", but as soon as I login as that user, it creates a whole other directory as "\Users\username.computername\". Need to find a way to set this so copy commands will work later in the script. –  BigRedEO Aug 13 '13 at 18:34
@BigRedEO net localgroup Administrators /add UserName Not need "md \Users\username" –  STTR Aug 14 '13 at 13:44
That will add a user to the Administrators group, thank you, but it doesn't solve my problem of the home directory of each user not being created until that user is first logged in. I'm thinking maybe it has something to do with the "Default" directory found in "\Users"? In other words, when you first login to a new account and it says "Preparing your desktop", what is actually running there to prepare the desktop. Could that give me what I need to activate the users Home directory? –  BigRedEO Aug 14 '13 at 14:09
@BigRedEO What you want - Profile Path. Then what was asked - Home Directory. Understand exactly what you need. –  STTR Aug 14 '13 at 15:42
@BigRedEO Updated, test script proinit.cmd. –  STTR Aug 15 '13 at 9:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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