hello

this is my second post about this. I used the below code on my test user and this worked wonderfully. but when i tried to use it on my CSV file full of 400 odd users it only outputted one samaccountname.

In my previous post the guy that got the best answer added New-Object to the end of the script but i didnt know if this would modify AD so was scared to use it.

$names = ipcsv C:\test\sarah.csv
ForEach ($Name in $Names)
{
  $FirstFilter = $Name.FirstName
    $SecondFilter = $Name.LastName
  $ThirdFilter = $Name.Department
      $user=Get-ADUser -Filter "GivenName -like '$FirstFilter' -and Surname -like '$SecondFilter'"
      if( $User )
    {
        $User |Select-Object samaccountname |Export-CSV -Path C:\test\output.csv -NoTypeInformation
    }
  } 

Any help would be greatly appreciated!

Many thanks!

Lawtey :slight_smile:

1 Spice up

This line here:

$User |Select-Object samaccountname |Export-CSV -Path C:\test\output.csv -NoTypeInformation

Notice it creates the file, then overwrites it. The one user left was the last user in the CSV, huh? Just need to add -Append to the Export-CSV cmdlet:

$User |Select-Object samaccountname |Export-CSV -Path C:\test\output.csv -NoTypeInformation -Append

it doesn’t like that at all :frowning:

Export-Csv : A parameter cannot be found that matches parameter name ‘Append’.
At line:16 char:108

  • $User |Select-Object samaccountname |Export-CSV -Path C:\test\output.csv -NoTypeInformation -Append <<<<
  • CategoryInfo : InvalidArgument: (:slight_smile: [Export-Csv], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

Looks like you are running Old version of powershell

either run as


```
$names = ipcsv C:\test\sarah.csv
$result=ForEach ($Name in $Names)
{
  $FirstFilter = $Name.FirstName
   $SecondFilter = $Name.LastName
  $ThirdFilter = $Name.Department
      Get-ADUser -Filter "GivenName -like '$FirstFilter' -and Surname -like '$SecondFilter'"
    }
  $result|Select-Object samaccountname |Export-CSV -Path C:\test\output.csv -NoTypeInformation
```

or

$names = ipcsv C:\test\sarah.csv
ForEach ($Name in $Names)
{
  $FirstFilter = $Name.FirstName
    $SecondFilter = $Name.LastName
  $ThirdFilter = $Name.Department
      $user=Get-ADUser -Filter "GivenName -like '$FirstFilter' -and Surname -like '$SecondFilter'"
      if( $User )
    {
        $User |Select-Object samaccountname |out-file -Path C:\test\output.csv -append
    }
  }