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

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

Initialize user home folder on Windows with commandline

问题

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

我希望尽可能使用本机的PowerShell来创建该文件夹,但如果有某种可用的exe文件,也可以。我需要它是非交互式的。

我已经查看了以下内容:

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.html
匿名

发表评论

匿名网友

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

确定