title: Powershell AD Users CSV report with ADPropertyValueCollection Error
body: <p>Got a loop going through AD users that will delete users that are over 90 days old.
  I want to pull a report of all the deleted users in to a CSV.
  In a few fields of the CVS I get the Microsoft.ActiveDirectory.Management.ADPRopertyValueCollection</p>
  
  <p>The code is this</p>
  
  <pre><code> Get all users in that have not logged on within 
  # 60 days in "Active Directory" and Disable them 
  # 
  # Get the Current Date 
  LogInfo("START OF LOG FILE")
  LogInfo("Compare Date : Getting date")
  $COMPAREDATE=GET-DATE
  
  # Number of Days to check back.
  LogInfo("Set Disable Time : Settings Number of days to Disable to 60")
  $NumberDays=(get-date).addDays(-60)
  #$then = (Get-Date).AddDays(-60)
  
  # Number of Days to check for REALLY Stale accounts 
  # Our sample here is taking "OldAccounts" and pumping up 
  # 30 more days.  
  #Therefore 90 days old accounts that haven't logged in should be purged 
  # 
  LogInfo("Set Delete Time : Setting number of days to Delete to 90")
  $DeleteDate=$NumberDays+30
  
  # We have certain "Override fields" that bypass a delete 
  # happening.  If the "Notes" field in A/D contains the 
  # EXACT Override phrase ANYWHERE (in this case it is the 
  # word ***OVERRIDE*** and it IS case sensitive 
  # The account will NEVER be deleted (unless of course you remove 
  # Word from the Notes field 
  #
  LogInfo("Set Override key word")
  #$OverRide='***OVERRIDE***' 
  
  # The other override field is if 
  # the OnLeave details are in the Description 
  # Field in A/D.  this allows for a User who is 
  # Not gone (IE: Contractor / Student) but may 
  # Return to have the account disabled and 
  # Left alone until they return.  The words here are 
  # simple On Leave Until and can be ANYWHERE in the 
  # Description Field in A/D 
  # 
  LogInfo("Set On Leave override key word")
  $OnLeave='On Leave Until'
  
  # Organizational Unit to search â€“ This is in the fictional domain of 
  # â€˜Contoso.localâ€™ in the OU of Users under the Business OU on the Root 
  # of the Contoso A/D 
  # 
  LogInfo("Set OU Path : Setting OU path to Test OU")
  $OU='OU=Users,OU=Test,DC=corporate,DC=nzpost,DC=co,DC=nz'
  
  # Get all users not active within the specified range and disable the accounts in Active Directory 
  # 
  # We store them away as a variable since we're going to examine the list a few times. 
  LogInfo("Listing User accounts that is 60 days old")
  $LISTOFACCOUNTS=Get-ADUser -Property lastlogondate -SearchBase $OU -Filter {lastLogonDate -lt $NumberDays}
  # 
  # Any account not logged in within the short range gets Disabled in AD 
  # 
  LogInfo("Disabling user accounts 60 days old")
  $LISTOFACCOUNTS | DISABLE-ADACCOUNT -WhatIf
  
  # Pull up a new list.   Really old accounts 
  # 
  #$LISTOFPOTENTIALDELETES=$LISTOFACCOUNTS | where { $_.LastLogon.AddDays($DeleteDate) -gt $CURRENTDATE } 
  $LISTOFPOTENTIALDELETES=Get-ADUser -SearchBase $OU -Property Lastlogondate -Filter {lastlogondate -lt $DeleteDate}
  
  # Secondary compare is more interesting.  If the accounts are VERY stale, they get deleted UNLESS special keywords 
  # are in place 
  # 
  FOREACH ($USER in $LISTOFPOTENTIALDELETES) { 
  
  
          Get-ADUser -Identity $USER -Properties * | Select @{ Name = 'ADsPath'; Expression = { $_.ADsPath -join ';'; }; },cn,givenName,lastLogonDate,description, profilePath, homeDirectory, `
              @{ Name = 'mail'; Expression = { $_.mail -join ';'; }; }, @{ Name = 'publicDelegates'; Expression = { $_.publicDelegates -join ';'; }; }, whenCreated, company, manager, employeeID, `
              @{ Name = 'memberof'; Expression = { $_.memberof -join ';'; }; }  | Export-CSV "E:\Damo\_UserList.csv" -Append  
  
      IF (($USER.Notes -notlike '*'+$OVERRIDE+'*') -and ($USER.Description -notlike '*'+$OnLeave+'*')) 
      { 
  
          LogInfo("$USER.SamAccountName Deleted") 
          WRITE-HOST $USER.SamAccountName 'Deleted' 
          REMOVE-ADOBJECT $USER.SamAccountName -whatif 
  
      } 
      ELSEIF ($USER.Notes -like '*'+$OVERRIDE+'*') 
          { 
              LogInfo("$USER.SamAccountName Not removed due to Administrative Override")
              WRITE-HOST $USER.SamAccountName 'Not removed due to Administrative Override'  
           } 
          ELSE 
          { 
              LogInfo("$USER.SamAccountName Not removed - Presently on Leave")
              WRITE-HOST $USER.SamAccountName 'Not removed - Presently on Leave' 
          } 
  
  #Get-ADUser -Identity $USER -Properties * | Select ADsPath,cn,givenName,lastLogonDate,description, profilePath, homeDirectory, @{ Name = 'mail'; Expression = { $_.mail -join ';'; }; }, 
      #publicDelegates, whenCreated, company, manager, employeeID, memberof | Export-CSV "E:\Folder\_UserList.csv" -Append
  } 
  
  $users = get-aduser -SearchBase $OU -Properties userPrincipalName,lastlogonDate,description,mail,profilePath,HomeDirectory -filter {userPrincipalName -like "*"} 
  $csv = foreach($user in $users){ 
  
      $grp = get-adprincipalgroupmembership $user 
      Foreach($group in $grp){ 
          New-Object -TypeName PSObject -Property @{ 
              #MemberOf = $user.memberof[0]
              Group = $group.Name 
              User = $user.SamAccountName 
              GivenName = $user.givenName
              Surname = $User.Surname
              LastLogon = $user.lastlogondate
              Description = $User.Description
              Mail = $User.Mail
              ProfilePath = $User.profilePath
              HomeDir = $User.homeDirectory
              } 
          } 
  } 
  
  $csv | Export-csv E:\Folder\DeletedUsersInfo.csv
  </code></pre>
  
  <p>How do I get the MemberOf to populate in the CSV, so it shows all the group for that user</p>
  
  <p>I have two loops there as I was trying to get one to work, so it just shows the memberof groups without the full OU Path.</p>
  
  <p>Any help would be fantastic.</p>
  
  <p>Cheers in advance</p>