PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Script blogs

Get-LocalGroupMember: Find Local admin using PowerShell (2021)

themeanmachine19@gmail.com by [email protected]
September 18, 2021
in Powershell Script blogs
2
Share on FacebookShare on Twitter

You might also like

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

May 21, 2022
PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

May 15, 2022

One day my boss asked me to provide the list of the local group members list from more than 1000 servers within 2 hrs. Not sure if he was taking any revenge on me but I was well versed with google (my solution to every problem) and I got to know about “get-localgroupmember” command. Today I am going to share how I create a script with step-by-step details.

Get LocalGroupMember

All about Get-LocalGroupMember

Get-LocalGroupMember is used to get the list of group members from a given group. Below is the syntax used for Get-LocalGroupMember.

				
					Get-LocalGroupMember [[-Member] <String>][-Name] <String>[<CommonParameters>]
				
			

Below is the example:

				
					PS C:\Users\dhrub> Get-LocalGroupMember -Group "Administrators"

ObjectClass Name                          PrincipalSource 
----------- ----                          --------------- 
User       TestPc\Administrator           Local           
User       TestPc\dhrub                   MicrosoftAccount
				
			

In the above example, we are fetching the users inside the Administrators group, which is quite self explanatory.

Getting Local Admin list from multiple servers using Get-LocalGroupMember

In this section I will guide you step-by-step how I reached my goal of extracting the list of local admin from multiple servers.

Step #1

				
					$a= Get-Content -path C:\servers.txt

## Reading the servers list from the text file using Get-Content ##
				
			

Step #2

				
					foreach($b in $a){

#statement1
#statement2
#statement3


}
				
			

If you want to run scripts on multiple servers Foreach loops are always required. In this script, we will use the same. If you want to learn more on Foreach click here.

Step #3

				
					Invoke-Command -ComputerName $b -HideComputerName -scriptblock {}
				
			

Invoke-command usually created a pssession on the remote server so that we can execute some sets of command remotely.

Step #4

				
					$member= 'Administrators'
Get-LocalGroupMember -name $member}|`
Select * -ExcludeProperty RunspaceId | `
select @{n="Computer";e="PScomputerName"},Name,PrincipalSource |`
export-csv C:\try3.csv -NoTypeInformation -Append

				
			

I have created a variable $member and assigned the value “Administrator” and you can assign any local group name in $member.

@{n=” computer”;e=”PScomputerName”}, this line will basically alter the output as it will change the name of the expression PScomputerName to just Computer.

Export-csv will convert the output in a .csv format.

-Append will append the results in the CSV. It will be required as generally if it is not there then the result will be the output of the end of the script.

Step #5

				
					$a= Get-Content -path C:\servers.txt

foreach($b in $a){

Invoke-Command -ComputerName $b -HideComputerName -scriptblock {
$member= 'Administrators'
Get-LocalGroupMember -name $member}|`
Select * -ExcludeProperty RunspaceId | `
select @{n="Computer";e="PScomputerName"},Name,PrincipalSource |`
export-csv C:\try3.csv -NoTypeInformation -Append

}
				
			

Above is the final script that I have created to resolve the problem, below is the output from the .csv file.

“Computer”,”Name”,”PrincipalSource”
“newvm”,”newvm\administrator1″,”Local”
“newvm”,”newvm\administrrator1″,”Local”
“newvm”,”TEST\Domain Admins”,”ActiveDirectory”
“newvm”,”TEST\test30″,”ActiveDirectory”
“newvm4″,”newvm4\administrator1″,”Local”
“newvm4″,”newvm4\administrrator1″,”Local”
“newvm4″,”TEST\Domain Admins”,”ActiveDirectory”
“newvm4″,”TEST\test30″,”ActiveDirectory”

Conclusion

This small script helped me a lot and save my day, now my boss is happy with me and I think he will be giving me more work in the future but I will share everything with you without remorse. If you like the post, please recommend our page on Facebook and leave a reply so that I can create more topics in the future. Also, let me know if you want to check out the video of this script.

Please login to join discussion
close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

by [email protected]
May 21, 2022
0

Instant guide to convert ps1 to exe. How to convert ps1 to exe. PS2exe. Convert ps1 to exe using ps2exe.

PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

by [email protected]
May 15, 2022
0

Useful Powershell tips and tricks. PowerShell tips and tricks. Know Powershell tips and tricks. get-help. get-alias.

tips to manage hyper-v using powershell

5 Tips To Manage Hyper-V Using PowerShell

by [email protected]
May 1, 2022
0

Tips to manage hyper-v using PowerShell. How to manage Hyper-V using PowerShell. Powershell to manage Hyper-V. Hyper-v and PowerShell.

Mistakes To Avoid While Writing A Powershell Script

5 Big Mistakes To Avoid While Writing A Powershell Script

by [email protected]
April 30, 2022
0

Mistakes to avoid while writing a powershell script. PowerShell mistakes. Avoid these mistakes in a powershell script. PowershellGuru.

Next Post
Transfer fsmo roles using PowerShell

Step-By-Step guide to transfer FSMO roles using PowerShell (2021)

Please login to join discussion

Recommended

How to use PowerShell to manage DNS records

How to use PowerShell to manage DNS records (2022)

April 2, 2022
Detect Chrome Zero-day vulnerable servers using PowerShell

Detect Chrome Zero-day vulnerable servers using PowerShell (2022)

February 18, 2022

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    827 shares
    Share 331 Tweet 207
  • Detect Log4j vulnerable servers using PowerShell

    753 shares
    Share 301 Tweet 188
  • How to find NTP Server using PowerShell?

    738 shares
    Share 295 Tweet 185
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    728 shares
    Share 291 Tweet 182
  • Get installed software list quickly using PowerShell (2021)

    695 shares
    Share 278 Tweet 174
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.