Powershell error adding to an array

Viewed 76498

Hi I am trying to run a script to get users in a network into an array so that it can be outputted to a csv file along with other data that I am going to get, such as distribution groups.

The code script I am running is giving an error:

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.

The script is a simple for loop:

$ActiveDirectoryList=@() 
$UserDetails = get-aduser -filter {enabled -eq $true} -properties * | Select DisplayName,EmailAddress, SAMAccountName
$counter = 0
foreach($User in $UserDetails){

    $ActiveDirectoryList = New-Object PSObject
    $Users = get-aduser $User.SAMAccountName -properties * 
    if(!$Users.EmailAddress -eq ""){
        $counter++
        $ActiveDirectoryList | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $Users.DisplayName
        $ActiveDirectoryList | Add-Member -MemberType NoteProperty -Name "Email Address" -Value $Users.EmailAddress 
        write-host $Users.DisplayName
        $ActiveDirectoryList+=$ActiveDirectoryList
    }
}

Tried looking on the internet for a solution but they don't seem to solve anything.

3 Answers

Additional Note: This can also occur if you don't declare the array as an array or mistype it when you declare it. Simple little error. But, maybe it will help someone that comes here and still can't get it resolved. Check your array variable name declaration carefully.

# Example 1 of how you could unintentionally fubar it.
$ActiveDirectoryList=$null
# Example 2 of how you could unintentionally fubar it.
$ActiveDirectoryList_ExaggeratedExampleWrongName=@() 
# Example 3. Same as 2 above, but not as easily identifiable type error
$ActiveDirectoryLists=@()


$UserDetails = get-aduser -filter {enabled -eq $true} -properties * | Select DisplayName,EmailAddress, SAMAccountName
$counter = 0
foreach($User in $UserDetails){

    $ActiveDirectoryObject = New-Object PSObject
    $Users = get-aduser $User.SAMAccountName -properties * 
    if(!$Users.EmailAddress -eq ""){
        $counter++
        $ActiveDirectoryObject | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $Users.DisplayName
        $ActiveDirectoryObject | Add-Member -MemberType NoteProperty -Name "Email Address" -Value $Users.EmailAddress 
        write-host $Users.DisplayName
        $ActiveDirectoryList += $ActiveDirectoryObject
    }
}

+= kills puppies. (the whole array is copied every time)

$UserDetails = get-aduser -filter {enabled -eq $true} -properties | 
  Select DisplayName,EmailAddress, SAMAccountName
$counter = 0

$ActiveDirectoryList = foreach($User in $UserDetails){

    $Users = get-aduser $User.SAMAccountName -properties * 
    if(!$Users.EmailAddress -eq ""){
        $counter++
        New-Object PSObject -property @{
          Displayname = $Users.DisplayName
          'Email Address' = $Users.EmailAddress
        }
        write-host $Users.DisplayName
    }
}

This can be simplified to:

$ActiveDirectoryList = get-aduser -filter {enabled -eq $true -and
  EmailAddress -like '*'} -properties Displayname,EmailAddress | 
  select Displayname,@{n='Email Address';e={$_.EmailAddress}}
Related