在命令行上初始化用户的 Windows 主文件夹

huangapple go评论87阅读模式
英文:

Initialize user home folder on Windows with commandline

问题

我在Windows 2019服务器上有一个本地用户,名为myuser,但没有主文件夹,也就是C:\Users\myuser

我更倾向于使用本机PowerShell来创建文件夹,但如果有某种可执行文件可用,也可以。我需要它是非交互式的。

我已经查看了:

New-LocalUser -Name myuser1 -Password (ConvertTo-SecureString Password123! -AsPlainText -Force)
Get-Command -Module Microsoft.PowerShell.LocalAccounts
net.exe user myuser2 /add Password123!
net.exe user myuser3 /add Password123! /homedir:C:\Users\myuser3

New-LocalUser 不能创建主文件夹。net.exe 也不能。而 Microsoft.PowerShell.LocalAccounts 模块中没有其他有用的命令。

请注意,我真正想要做的是在用户已经存在时初始化主目录,而不是创建一个新的。

我的唯一选择似乎是交互登录,或者交互式使用 runas.exe /profile /user:myuser whoami.exe(这将提示输入密码)。

手动创建主目录,例如 mkdir C:\Users\myuser,会创建一个实际上并不成为用户主目录的文件夹。如果用户在 mkdir 后进行交互登录,会创建一个类似 C:\Users\myuser.MYCOMPUTER 的真正主目录。

英文:

I have a local user on a Windows 2019 Server, myuser, but no home folder AKA C:\Users\myuser.

I preferably want to use native PowerShell to create the folder, but if there is some kind of exe file I could use, that is fine. I need it to be non-interactive.

I've looked at:

New-LocalUser -Name myuser1 -Password (ConvertTo-SecureString Password123! -AsPlainText -Force)
Get-Command -Module Microsoft.PowerShell.LocalAccounts
net.exe user myuser2 /add Password123!
net.exe user myuser3 /add Password123! /homedir:C:\Users\myuser3

New-LocalUser provides no ability to create the home folder. Neither does net.exe. And there is no other usefull commands in the Microsoft.PowerShell.LocalAccounts module.

Note that what I'd really like to do is initialize the home directory when the user already exists, not create a new one.

My only options seem to be interactive logon, or interactive runas.exe /profile /user:myuser whoami.exe (this will prompt for password).

Creating the home manually, e.g. mkdir C:\Users\myuser, creates a folder that doesn't actually become the users home. If the user interactively log on after mkdir something like C:\Users\myuser.MYCOMPUTER is created as the real home folder.

答案1

得分: 2

如果您以创建的用户身份启动一个进程(cmd /c),它将创建他的个人配置文件。

$Cred = [System.Management.Automation.PSCredential]::new("myuser", (ConvertTo-SecureString "Password123!" -AsPlainText -Force))

Start-Process "cmd.exe" -Credential $Cred -ArgumentList "/C"
英文:

If you start a process (cmd /c) as the created user, it will create his profile.

$Cred = [System.Management.Automation.PSCredential]::new("myuser", (ConvertTo-SecureString "Password123!" -AsPlainText -Force))

Start-Process "cmd.exe" -Credential $Cred -ArgumentList "/C"

huangapple
  • 本文由 发表于 2023年8月9日 16:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866012-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定