$Results = @()
$Users = Import-CSV C:\Filter\test1.CSV -Header First,Last,Email
Foreach($User in $Users){
$UserProps = get-aduser -Filter "GivenName -eq '$($user.GivenName)' -and Surname -eq '$($user.Surname)'" -Properties GivenName,Surname,EmailAddress,Department
$Results += New-Object PSObject -Property @{
FirstName = $User.GivenName
LastName = $User.Surname
EmailAddress = $User.EmailAddress
Department = $UserProps.Department}
}
$Results
This is test1.CSV 3 columns include:
First Last Email
Westley XXX [email protected]
Dave XXX [email protected]
Gareth XXX [email protected]
Paul XXX [email protected]
What I want it to do is run through the CSV and pull active account information from PowerShell, however I dont think its doing that as my name should be printing out something else
It should be printing out DavidJ as a test since I've edit my account, but instead its printing Dave as my first name how it is in the spreadsheet (CSV).
get-aduser : The search filter cannot be recognized At line:2 char:18 + $UserProps = get-aduser -Filter "GivenName -eq '$($user.GivenName)' -and Sur ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Expected output:
EmailAddress FirstName Department LastName
------------ --------- ---------- --------
[email protected] westley mydepartment hislastnamefromAD
[email protected] davidj (from AD) mydepartment mylastnamefromAD
[email protected] gareth mydepartment hislastnamefromAD
[email protected] paul mydepartment hislastnamefromAD