Get Administrator group users

Source: Internet
Author: User

Get local administrator group users

Method 1:

$ Strcomputer = $ ENV: computername
$ Computer = [ADSI] ("winnt: //" + $ strcomputer + ", computer ")
$ Group = $ computer. psbase. Children. Find ("Administrators") # You can customize the group name.
$ Members = $ group. psbase. invoke ("members") | % {$ _. getType (). invokemember ("name", 'getproperties', $ null, $ _, $ null )}
$ Members

 

Method 2:

[String] $ computername = $ ENV: computername
$ Computer = [ADSI] "winnt: // $ computername"
$ Admingroup = "Administrators" # You can customize the group name.
$ Group = [ADSI] "winnt: // $ computername/$ admingroup"
$ Members = $ group. psbase. invoke ("members") | % {$ _. getType (). invokemember ("name", 'getproperties', $ null, $ _, $ null )}
$ Members

 

Method 3:

$ Group = [ADSI] "winnt: //./administrators, group"
$ Members = $ group .. psbase. invoke ("members") | % {$ _. getType (). invokemember ("name", 'getproperties', $ null, $ _, $ null )}
$ Members

$ Members. Contains ("$ user") is used to determine whether $ user is included. True is true if $ user is returned.

 

Method 4 (batch processing ):

 

Net localgroup Administrators

 

 

Method 5: Convert vbs to powershell.

[Reflection. Assembly]: loadwithpartialname ("'Microsoft. VisualBasic ")
$ A = [microsoft. VisualBasic. Interaction]: GetObject ("winnt: //./administrators ")
$ A | % {$ _. GetType (). invokemember ("name", 'getproperties', $ null, $ _, $ null )}

Result: Administrators

 

 

 

Determine whether the current logon user belongs to the Administrator group:

Whether the current user belongs to the Administrator group:

Method 1:

$ Currentuser = [Security. Principal. windowsidentity]: getcurrent ()
(New-Object Security. Principal. windowsprincipal $ currentuser). isinrole ([Security. Principal. windowsbuiltinrole]: Administrator)

Method 2:
Whoami/groups/fo csv | convertfrom-CSV | where {$ _. Sid-EQ's-1-5-32-544 '})-ne $ NUL

Whoami/groups

Get Local Users:

Gwmi win32_useraccount

Get local group:

Gwmi win32_group

 

ADSI:

$ User = [ADSI] "winnt: // $ computername"
$ User. Delete ("user", $ username)

Add User:

Net user ABC abcpassword/Add
Net localgroup administrators ABC/Add

Delete all administrator accounts (. vbs) other than administrator ):

Strcomputer = "."
On Error resume next
Set ogroupadm = GetObject ("winnt: //" & strcomputer & "/administrators ")
For each oadmkguser in ogroupadm. Members
Sadmreach user = lcase (oadmreach user. Name)
Remmsgbox sadmgr user
If (sadmgr user <> "Administrator") and (sadmgr user <> "Domain Admins") and (sadmgr user <> "Administrator") then
Msgbox sadmkguser
Ogroupadm. Remove oadmkguser. adspath
End if
Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.