I've got a Powershell script written at work mass-disabling users based on the given username in a CSV.
This week I was asked to do a bulk job but was only given e-mail addresses so I amended the script & CSV.
What I'd like to do now is to keep the CSV with headers "Username" & "Email" and then have the script find the Distinguished Name based on whichever value is given.
Eg.:
Here is the script I've currently got that has got both values in the script (script modified slightly to remove sensitive data). The script has to search on two different servers as we have users across two domains:
$Imported_csv | ForEach-Object {
$UserDN = $null
$TargetOU = $Null
$Server= $null
# Retrieve Username Address of user.
$User = $_.UserName
# Retrieve E-mail Address of User
$Email = $_.Email
# Retrieve DN of user based on e-mail address
$UserDNxxx = (Get-ADUser -Server xxx -Filter {mail -eq $Email}).distinguishedName
$UserDNxxx = (Get-ADUser -Server xxx -Filter {mail -eq $Email}).distinguishedName
# Retrieve DN of user based on Username address
$UserDNxxx = (Get-ADUser -Server xxx -Identity $_.UserName).distinguishedName
$UserDNxxx = (Get-ADUser -Server xxx -Identity $_.UserName).distinguishedName
Any tips would be greatly appreciated.

$Importedcsv? - Nico NekoruIf. If the username has a value, use that. Otherwise, try email address. - Gabriel Luci$import = $DataSource+"\Disable Users.csv" $Imported_csv = Import-Csv -Path $importI do believe an If statement is the answer. But my Powershell experience is very limited and I'm not entirely sure/confident how to construct it properly. - Nick Francis