I would like to use PowerShell to add a specific user to the local administrator group on a machine. I would be running the PowerShell script in the context of a user that has Administration rights on the local machine.
I would like to use PowerShell to add a specific user to the local administrator group on a machine. I would be running the PowerShell script in the context of a user that has Administration rights on the local machine.
On Server 2016 and Windows 10 Version 1607 and later you can use the new PowerShell local user cmdlets:
This was added in Windows Management Framework (WMF) 5.1.
The
Microsoft.PowerShell.LocalAccounts
module works fine on 2012 R2 if you just copy the files into a$env:PsModulePath
location.Here is a simple 2 line script that performs this function
For more information see Hey, Scripting Guy! How Can I Use Windows PowerShell to Add a Domain User to a Local Group?
So there are a couple of notes. In the first line I used string concatenation, I didn't have to (see the next line) but I like to because it helps accentuate the variables I am using. Second, these lines will add a domain user, if you wanted to add a local user just remove
$env:USERDOMAIN/
This is the Advanced Function That I use to add a users to the local Administrator group using Powershell on several computers.
Usage: Get-Content C:\Computers.txt | Set-LocalAdminGroupMembership -Account 'YourAccount'
Simple Step to add a domain user to the Administrators group:
Note: Make sure you run PowerShell "As Administrator".
Here is another way to do this. This needs to be run in Administrator context:
More info on my website.
Below is the snippet I use to add a user to the local administrators group that works on older versions of PowerShell for Windows Servers prior 2016. The code example adds a service account used for custom IIS AppPool identity to the local Administrators group.
Credit for using
net localgroup administrators
in the if statement above goes to this blog post.