如何使用PowerShell将用户名添加到管理员?

时间:2019-09-19 09:15:32

标签: powershell

如何使用Powershell在PC上让自己成为管理员?

例外:

Cannot find group name

+ $AdminGroup.Add($InUser.Path)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember
$User = $env:USERNAME
$HostName = $env:COMPUTERNAME
$DomainName = "xxxx"
$AdminGroup = [ADSI]"WinNT://$HostName/administrators,group"
$InUser = [ADSI]"WinNT://$DomainName/$User,user"
$AdminGroup.Add($InUser.Path)

1 个答案:

答案 0 :(得分:1)

您可以通过向BUILTIN \ Administrators组SID查询Win32_Group来找到组名:

$AdministratorsGroupName = (Get-CimInstance -ClassName Win32_Group -Filter 'SID = "S-1-5-32-544"').Name
# ...
$AdminGroup = [adsi]"WinNT://$HostName/$AdministratorsGroupName,group"