# Add AD account to local administrator group on a remote computer

**URL:** <https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395>\
**Category:** PowerShell Help\
**Created:** [August 23, 2021, 8:39pm UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395 "2021-08-23T20:39:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GUaro5555](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@GUaro5555](https://forums.powershell.org/u/GUaro5555)\
**Post date:** [August 23, 2021, 8:39pm UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/1 "2021-08-23T20:39:02Z")

</div>

Hi gurus.  
I am very new to powershell and I am looking for a script that will help me add an account in multiple computers at the same time using a csv file., is this possible?

Thanks gurus

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 23, 2021, 8:55pm UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/2 "2021-08-23T20:55:27Z")

</div>

> [@GUaro5555](#):
>
> is this possible?

It definitely is. 👍🏽 😉

You will need the following cmdlets:

> **[Import-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including...

> **[New-PSSession (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssession?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The New-PSSession cmdlet creates a PowerShell session (PSSession) on a local or remote computer. When you create a PSSession, PowerShell establishes a persistent connection to the remote computer. Use a PSSession to run multiple commands that share...

> **[Invoke-Command (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote...

> **[Add-LocalGroupMember (Microsoft.PowerShell.LocalAccounts) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/add-localgroupmember?view=powershell-5.1)**
>
> The Add-LocalGroupMember cmdlet adds users or groups to a local security group. All the rights and permissions that are assigned to a group are assigned to all members of that group. Members of the Administrators group on a local computer have Full...

It might help using your avorite search engine to search for examples. I’m pretty sure there are a lot of. 😉

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [August 23, 2021, 11:42pm UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/3 "2021-08-23T23:42:46Z")

</div>

I have also done this with no issues … maybe not the best way, and not 100% PowerShell, but it worked for me in one off situations.

```auto
Invoke-Command -ComputerName 'hostname' -ScriptBlock {net localgroup Administrators /add 'domainaccount' /domain}

```

The main issue with this is Remote PowerShell can be a pain depending on your domain security. You might want to have a look at adding the account(s) via GPO.

> **[How To Add Local Administrators via GPO (Group Policy)](https://thesysadminchannel.com/add-local-administrators-via-gpo-group-policy/)**
>
> Add Local Administrators via GPO - Ill show you the steps needed to add users or groups as local admins using Group Policy.

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [August 25, 2021, 2:57am UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/4 "2021-08-25T02:57:05Z")

</div>

Hello Guaro,

You can try this.  
first, you need a csv file with Computername,ADaccount like this.  
list.csv

## Computername,ADaccount

Client,Domian\user1  
Client2,Domian\user2  
Client3,Domian\user3  
The use these commands

```auto
$lists = Import-csv -path "the list.csv path "
foreach($list in $lists){
        try{
        Invoke-Command -ComputerName $list.computername -ScriptBlock {param($adaccount)Add-LocalGroupMember -Group "administrators" -Member $adaccount} -ArgumentList $lists.ADaccount
        }
        catch{
            write-host "$list.computername add $lists.ADaccount to Administrators group filed "
        }
}

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 25, 2021, 3:12am UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/5 "2021-08-25T03:12:29Z")

</div>

This will not work. $lists is not defined in the remote scriptblock

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [August 25, 2021, 5:07am UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/6 "2021-08-25T05:07:03Z")

</div>

@krzydoug thank you for remind. I have add the -ArgumentList.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/add-ad-account-to-local-administrator-group-on-a-remote-computer/17395/7 "2024-05-16T20:21:44Z")

</div>


