Use CSV file with Names to get ADUser Email Addresses - powershell

First, I would like to thank you for your help
This is my question
I've got a CSV file with one Line
DisplayName
John Doe
Jane Doe
And I would like to use this Names so I get their EmailAdresses from our AD
This is the code
Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $users) {
Get-ADUser -Filter "name -eq '$user.DisplayName'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties name, emailAddress | Select-Object Name, emailAddress
}
$List = Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8
But it is not working
Thanks a lot

As stated by #Abraham Zinala said, your code doesn't show you capturing your csv import into a variable. I'm assuming that's a typo/copy issue.
The issue I see in your code is you are referencing a property of the variable, which requires a subexpression
$userlist = Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $userlist) {
Get-ADUser -Filter "name -eq '$($user.DisplayName)'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties emailAddress | Select-Object Name, emailAddress
}
$List | Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8
You could also dereference the property as part of the foreach statement which would make the simple variable expand fine without subexpression.
$userlist = Import-Csv C:\Temp\test.csv
$List = Foreach ($user in $userlist.Displayname) {
Get-ADUser -Filter "name -eq '$user'" -SearchBase 'OU=Users,OU=Test,DC=Test,DC=local' -Properties emailAddress | Select-Object Name, emailAddress
}
$List | Export-CSV c:\temp\allinfo.csv -NoTypeInformation -Encoding UTF8

Related

Powershell get aduser query by the emailaddress

I'm trying to get the AD user list query by email address. I managed to get the report however, I can't export it to a CSV file. I don't know which part gets wrong.
really much appreciate it if someone can guide me.
Thanks.
here is the script I made.
$ADUsers = Import-csv "C:\temp\DevicesWithInventory_6db9330a-4377-4057-bc86-837f55fee3f6.csv"
$email= $user.Primaryuseremailaddress
foreach ($user in $ADUsers) {
get-aduser -Filter {emailaddress -eq $email} -Properties * | select name,mail, whencreated, company,cn,country
} | Export-Csv -path c:\temp\intune_add_country.csv -NoTypeInformation
this is the amended script
$ADUsers = Import-csv "C:\temp\DevicesWithInventory_6db9330a-4377-4057-bc86-837f55fee3f6.csv"
$result = ForEach ($user in $ADUsers) {
$email= $user.PrimaryuserUPN
get-aduser -Filter {emailaddress -eq $email} -Properties * | select name,mail, whencreated, company,cn,country
}
$result| Export-Csv -path c:\temp\intune_add_country.csv -NoTypeInformation
despite the error, I get the report result that I want.

To obtain the attribute from the CSV list

There is a list of users in CSV:
Name
surname name
I want to get to their posts. I use this script:
$userList = Import-Csv "C:\Scripts\names.csv"
foreach ($User in $userList) {
Get-ADUser -Identity $user.Name -Properties DisplayName, title |
select DisplayName, title |
Export-Csv -Append "C:\Scripts\title.csv" -NoTypeInformation
}
But the script does not find users.
If you are trying to get the users by their Name or DisplayName you will have to go with Filter instead of Identity. The following should work, assuming your actual AD-users Names are in the format of the csv.
$userList = import-csv "C:\Scripts\names.csv"
ForEach($User in $userList){
Get-ADUser -Filter "Name -eq '$($user.Name)'" -Properties DisplayName,title |
select DisplayName,title | Export-CSV -Append "C:\Scripts\title.csv" -NoTypeInformation}

Powershell Get-ADUser and variables

I am trying to write a Powershell script that will allow me to query the samaccountname and other fields from a list of e-mail addresses. (reverse lookup)
My email.csv file has a users header, followed by the list of e-mail addresses.
What am I doing wrong?
$users = Get-Content .\email.csv
$users | ForEach-Object {
Get-ADUser -LDAPFilter "(emailaddress=$users)" -Properties samaccountname | Select-Object -Property samaccountname
} | Export-Csv -Path .\emailexport.csv
you will need to change $_.emailaddress depending on your column title in your csv file. also, samaccountname is returned by default for get-aduser, so you can leave that part off.
$users = Get-Content .\email.csv
$users | ForEach-Object {
$email = $_.emailaddress
Get-ADUser -LDAPFilter "(emailaddress=$email)" -Properties emailaddress |
Select-Object -Property samaccountname, emailaddress
} | Export-Csv -Path .\emailexport.csv

get displayname and office from samaccountname export to csv

How I can get the displayname and the office from a samaccountname list (.txt)? After that I want to save the displaynames and the offices to a .csv file. Here is a approach:
$users = Get-Content C:\TMP\test.txt
foreach ($user in $users)
{
Get-ADUser -ldapfilter "(samaccountname=$user)" -Property name, office | Select-Object -Property Name, Office
}
It should look like:
Hope you can help me?
You are asking for the Export-CSV command but from your comments you might be having issues with placement or your construct of foreach.
Lets try this then
$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation
Update from comments
Was having issues understanding your output from the comments which was why I wanted more that just a picture of the headers. I don't an issue with this code and the text should be quoted so special characters, like commas, should not be an issue. Please update you question with the text content of a sample file and your PowerShell version in case that is coming into play.
Use the delimiter parameter at the end
$users = Get-Content C:\TMP\test.txt
$users | ForEach-Object {
Get-ADUser -ldapfilter "(samaccountname=$_)" -Property name,office | Select-Object -Property Name,office
} | Export-Csv -Path C:\temp\export.csv -NoTypeInformation -Delimiter ";"

Powershell Import-CSV and Foreach-Object, excluding with the CSV header

I'm trying to get the SAMAccountNames of one domain and compare them with their equals from another domain.
To get all users of dc1 I use:
Get-ADUser -Filter * -SearchBase $SearchBase | Select-Object SamAccountName |
Export-Csv -path $exports -encoding "unicode" -notype
and then I import the csv again and try to compare them for any differences
$readthat = Import-CSV $exports -Header SamAccountName | ForEach-Object {
$user1 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes
$user2 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes -Server $dc2
$modified = #{}
$attributes | Where-Object { $user1.$_ -ne $user2.$_ } | ForEach-Object {
$modified[$_] = $user2.$_
}
}
All that works great, except that it's also trying to find the SamAccountName which of course genereates an error because the SamAccountName = SamAccountName doesn't exit.
Any hints on how to avoid this or do you guys have a more elegant solution?
the .csv looks like this:
"SamAccountName"
"foo"
"bar"
Don't use the -Header SamAccountName option on your import-csv should help immensely. The -Header option is for when the CSV file you are importing doesn't have a header. The Export-CSV cmdlet puts the header in there for you, so you don't have to.