I wrote this code which works except for when I tried to add this: -and '$($user.'Manager Email' -notlike '')' to the end of the Get-ADUser Manager section which causes it to fail. What I would like to accomplish is to leave the Manager field blank in AD if the "Manager Email" field is blank within the CSV file. Any help would be greatly appreciated.
$Users = Import-CSV C:\AD-Test\HROrgData202207_2.csv
foreach ($User in $Users) {$AdUser = Get-ADUser -Filter "EmailAddress -eq '$($user.'Business Email')' -and Enabled -eq '$True'" -Properties mail -ErrorAction SilentlyContinue
$manager = Get-ADUser -Filter "EmailAddress -eq '$($user.'Manager Email')' -and '$($user.'Manager Email' -notlike '')'" -Properties mail -ErrorAction SilentlyContinue
if ($adUser) { Set-ADUser -Identity $ADUser -Department $user.Department -Title $user.Job -Office $user.Location -Description $user.Position -EmployeeID $user.'Employee ID' -Company $user.'Legal Entity' -Manager $manager }
else { Write-Warning "User $($User.'Business Email') could not be found" }