在 Powershell 中使用自定义用户文件夹创建本地用户

Far*_*lan 3 profile powershell scripting user-accounts windows-server-2008

我正在尝试创建一个新的 Win 2008 服务器本地用户并为该用户分配不同的配置文件路径。我不希望 Windows 在 C:\Users\newUser 下生成所有文件,相反,我希望它在 D:\customDir\newUser 中生成。有谁知道这样做的方法吗?

到目前为止,这就是我所拥有的:

$users= @("U1","U2","U3")
$computer = [ADSI]"WinNT://$env:COMPUTERNAME,computer"
$group = [ADSI]"WinNT://$env:COMPUTERNAME/MyCustomGroup,group"
$users | foreach {
    $userName = $_
    $userPath = "D:\customDir\$userName"
    echo "Creating $userName..."
    $user = $computer.Create("User",$userName)
    $user.put("description","User $userName Description")
    #$user.put("profilePath",$userPath) #This does not work, throws an error
    #$user.put("homeDirDrive","D:") #This appears to be ignored when uncommented
    $user.setpassword($userName)
    $user.setInfo()

    $group.add($user.Path)

    #run cmd from user account to create profile files/folders
    $spw = ConvertTo-SecureString $userName -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$spw
    Start-Process cmd /c -WindowStyle Hidden -Credential $cred -ErrorAction SilentlyContinue

}
Run Code Online (Sandbox Code Playgroud)

该脚本成功创建用户并将其添加到自定义组,但所有文件/文件夹最终都在 C:\Users\U*

Sch*_*ins 5

对于 OP,

感谢这篇文章,我试图弄清楚如何使这项工作适合我:

从用户帐户运行 cmd 以创建配置文件/文件夹

$spw = ConvertTo-SecureString $userName -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$spw
Start-Process cmd /c -WindowStyle Hidden -Credential $cred -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)

但我必须修改最后一行才能让它工作:

Start-Process cmd /c -Credential $cred -ErrorAction SilentlyContinue -LoadUserProfile
Run Code Online (Sandbox Code Playgroud)