在Powershell中使用自定义用户文件夹创建本地用户 [英] Create Local User with Custom User Folder in Powershell

查看:187
本文介绍了在Powershell中使用自定义用户文件夹创建本地用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新的Win 2008 Server本地用户,并为该用户分配其他配置文件路径.我不希望Windows生成C:\ Users \ newUser下的所有文件,而是希望它在D:\ customDir \ newUser中执行此操作.有人知道这样做的方法吗?

I am trying to create a new Win 2008 server local user and assign the user a different profile path. I don't want Windows to generate all the files under C:\Users\newUser, instead, I'd like to have it do that in D:\customDir\newUser. Does anyone know of a way to do this?

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

So far this is what I have:

$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

}

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

The script successfully creates the users and adds them to the custom group, but all the files/folders end up in C:\Users\U*

推荐答案

我为您想到的唯一一件事就是在注册表级别进行更改.请注意,我不建议您将注册表弄乱,但这确实可以满足您的要求-

The only thing I can think of for you would be to change it at the registry level. Note that I'm not recommending you mess with the registry, but this does do what you want -

新配置文件的默认目录由HKLM中的ProfilesDirectory确定:\ Software \ Microsoft \ Windows NT \ CurrentVersion \ ProfileList,默认情况下为%SystemDrive%\ Users,将其更改为$ userpath应该可以执行您想要的操作-

The default directory for a new profile is determined by the ProfilesDirectory in HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList, and by default is %SystemDrive%\Users, changing it to $userpath should do what you want -

$regpath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList"
$regname = "ProfilesDirectory"

set-itemproperty -path $regpath -name $regname -value $userpath

希望有帮助!

这篇关于在Powershell中使用自定义用户文件夹创建本地用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆