# How to feed an array of items into Add-LocalGroupMember

**URL:** <https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097>\
**Category:** PowerShell Help\
**Created:** [August 8, 2022, 12:00am UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097 "2022-08-08T00:00:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 8, 2022, 12:00am UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/1 "2022-08-08T00:00:44Z")

</div>

Hello,  
I have this code, I can’t figure out how to get the array into the group. I know the top works , if I print out the array before the invoke-command part, it prints out the names I entered. But it is giving an error:

```auto
What Computer do you want to add users to local admin group on?: server3
What users do you want to add?: tom
What users do you want to add?: dick
What users do you want to add?: harry
What users do you want to add?: 
Cannot validate argument on parameter 'Member'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    + CategoryInfo : InvalidData: (:) [Add-LocalGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand
    + PSComputerName : server3

```

Here is the script:

```auto
$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
   Add-LocalGroupMember -Group "Administrators" -Member $Userarray
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 8, 2022, 12:15am UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/2 "2022-08-08T00:15:14Z")

</div>

Here is 1 of my attempts that fails: I have local user accounts called tom, dick, and harry

```auto
$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $User
}
}

```

---

<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 8, 2022, 8:13am UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/3 "2022-08-08T08:13:59Z")

</div>

You are trying to use a variable inside a script block you defined outside of it. Your issue is about scope.

You may read the following help topics completely including the examples to learn how to deal with scopes.

> **[about Scopes - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2#the-using-scope-modifier)**
>
> Explains the concept of scope in PowerShell and shows how to set and change the scope of elements.

> **[about Scopes - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2#example-5-using-a-local-variable-in-a-remote-command)**
>
> Explains the concept of scope in PowerShell and shows how to set and change the scope of elements.

> **[about Script Blocks - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7.2#short-description)**
>
> Defines what a script block is and explains how to use script blocks in the PowerShell programming language.

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 8, 2022, 1:16pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/4 "2022-08-08T13:16:32Z")

</div>

This seems like it should work, but it doesnt do anything and I get no error:

```auto
$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($Using:user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $User
   
}
}

```

Ive tried changing the $User variable but still not working. Is $User referenced the same way inside the Invoke command after the $Using:User?

---

<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 8, 2022, 1:53pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/5 "2022-08-08T13:53:36Z")

</div>

> [@matt1974](#):
>
> Ive tried changing the $User variable but still not working.

`$User` is **not** the variable you define OUTSIDE of the scriptblock. 😉

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 8, 2022, 3:21pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/6 "2022-08-08T15:21:14Z")

</div>

Still no luck.

Here is my code:

```auto
$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')
write-host "before scriptblock"
$Computer
$input

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($Using:user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $input
    }
   Get-LocalGroupMember Administrators
    hostname
    Write-Host "Inside ScriptBlock"
   $computer
   $input
   $user
    }
    Write-Host "Outside ScriptBlock"
    $computer
   $input
   $user

```

Output:

What Computer do you want to add users to local admin group on?: server3  
What users do you want to add?: tom  
What users do you want to add?: harry  
What users do you want to add?:  
before scriptblock  
server3  
tom  
harry

Inside ScriptBlock  
Outside ScriptBlock  
ObjectClass Name PrincipalSource PSComputerName

* * *

Group BASEMENT\Domain Admins ActiveDirectory server3  
User BASEMENT\Guest ActiveDirectory server3  
User BASEMENT\SERVER2$ ActiveDirectory server3  
User SERVER3\Administrator Local server3  
server3  
server3

🤔

---

<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 8, 2022, 3:35pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/7 "2022-08-08T15:35:11Z")

</div>

_ **`$USERARRAY`** _ is the variable you define outside the script block and use inside of it. 🤷🏽‍♂️ 🙄

```auto
$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
    $User = Read-Host -Prompt "What users do you want to add?"
    if ($User -ne '') { $Userarray += $User }
}
until ($User -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock {
    Add-LocalGroupMember -Group Administrators -Member $USING:userarray
}

```

And BTW: it usually is a bad idea to accept free text input for such a use case. A better option would be to offer a limitted choice the user can choose from.

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 8, 2022, 3:39pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/8 "2022-08-08T15:39:52Z")

</div>

oh so you do not need the foreach then, it just feeds the entire array in.

Thanks!

---

<div class="post-metadata">

**Author:** ![PoshDad](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/poshdad/32/2626_2.png) [@PoshDad](https://forums.powershell.org/u/PoshDad)\
**Post date:** [August 10, 2022, 8:39pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/9 "2022-08-10T20:39:18Z")

</div>

I agree, params with validate set would be ideal.

---

<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:16pm UTC](https://forums.powershell.org/t/how-to-feed-an-array-of-items-into-add-localgroupmember/20097/10 "2024-05-16T20:16:45Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


