Hi All

i have a single csv file which contains samaccount names and upns. i mean some are samaccountnames and some ups in the same csv file. i am trying the below syntax but i am not getting the output. experts guide me.

$list = Import-Csv C:\temp\input.csv
 ForEach($user in $list){    
 $dn = $user.user
 Get-ADUser -Filter $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co  |Export-Csv C:\temp\output.csv -NoType -Append }
10 Spice ups

The CSV needs to have a header line. In this case if the header is named “user”, this code should work. However, if the DN’s have spaces in them, you need to wrap each one in double quotes in your CSV first. Example of CSV data below.

EDIT: Oops, you said UPN not DN. It depends on the format of your UPN. In this example, mine is janedoe@contoso.com. You can script this off to fetch the user’s DN.

user
jdoe
janedoe@contoso.com
$list = Import-Csv C:\temp\input.csv
 ForEach($user in $list){
 $dn = if($user -notmatch '@'){(get-aduser $user).distinguishedname}else{(Get-ADUser $user.user.Replace("@contoso.com","")).distinguishedname}  
Get-ADUser $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co  |Export-Csv C:\temp\output.csv -NoType -Append }
1 Spice up

A try catch will work too I believe.

$list = Import-Csv C:\temp\input.csv
 ForEach($user in $list){ 
 $UPN = $user.user.ToString() 
 $data = Try{Get-ADUser $user.user -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office}
         catch{  Get-ADUser -Filter { UserPrincipalName -Eq  $UPN } -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office}  

$data | Export-Csv C:\temp\output.csv -NoType -Append

 }

2 Spice ups
$list = Import-Csv C:\temp\input.csv
 ForEach($user in $list){    
 $dn = $user.user
 Get-ADUser -Filter $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co  |Export-Csv C:\temp\output.csv -NoType -Append }

This is very close.
You haven’t defined the filter correctly in your sample code above.
-Filter {SomeAttribute Some Value}

So, you possibly want:

-Filter {SamAccountName -eq $dn -OR UserPrincipalName -eq $dn}
3 Spice ups

Ahh, I forgot about -or being an option.

1 Spice up

Just as a style note, your Get-ADUser command line is going to become cumbersome.

To break out the definitions and clean up the code, you can put the parameters for Get-ADUser into variables thus:

$NotWanted = @(
	"cctvdvr",
	"DiscoverySearch",
	"FAX_MAILER",
	"FederatedEmail",
	"IUSR",
	"noreply",
	"Scanner",
	"Spam Hero",
	"Support",
	"SystemMailbox")  # Eliminate duplicates by using regex wildcards

[RegEx]$Exclusions = ($NotWanted -Join "|")  # Build the RegEx at runtime

$WantedFields = @(
	'Name',
	'Initials',
	'MobilePhone',
	'OfficePhone',
	'UserPrincipalName')
$ADParms = @{
	Filter = {
		(Enabled -ne $False) -and 
		(Initials -like '*')
		}
	Properties = $WantedFields
	}  # Parameters for the Get-ADUser to follow

##			Roll through the list, dumping them all
(Get-ADUser @ADParms | Where-Object {$_.Name -NotMatch $Exclusions} | Sort Initials) |
 ForEach {
#[... do your thing ...]
}

I know this isn’t an answer to your question, but I think you’ll soon find it useful.