Powershell get aduser query by the emailaddress - powershell

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.

Related

Checking ExtensionAttributes

I had a list of 50 users with the Active Directory extensionAttribute12 sent to me, I was told that extensionAttribute13 was mixed up. So far, as I go through and check in AD, I don't see this to be so.
I would like to use PowerShell to check the list he gave me and export to my own list without going one by one.
I have this and when I runs it seems to export all users, but in my list, I only have 1 user. Later, I would like to run on the list of 50. I don't understand why I get all of the users.
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Import-Csv C:\Users\rhyman\Documents\ExListTest.csv | ForEach {
Get-ADUser -Filter * -Properties UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName | `
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 | `
Export-CSV c:\allinfo.csv -NoTypeInformation
}
}
I've done some editing, this is what I have now:
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Identity "$UserList" -Properties UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName |
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 |
Export-CSV C:\Users\RHyman\Documents\allinfo.csv -NoTypeInformation
}
What I'm most concerned with is that the Get-Aduser has error:
Cannot find an object with identity: '#{UserPrincipalName=first.last#x.x.gov
It sees the name on the list but I may have to go to the csv file and change the way the list displays the names.
Revised script: Only thing now exported csv is empty
Import-Module ActiveDirectory
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Filter {(mail -eq "$UserList")} -Properties
UserPrincipalName, extensionAttribute12, emailAddress, SAMAccountName |
Select UserPrincipalName, extensionAttribute12, extensionAttribute13 |
Export-CSV C:\Users\RHyman\Documents\allinfo.csv -NoTypeInformation
}
Finally got what I was looking for and it does what I wanted
This is what I needed and used Get-Content
Get-ADUser -Filter {mail -like $_}
Thanks for all the help!
You are passing entire csv [Get-ADUser -Identity "$UserList"] into Identity use below instead.
$UserList=Import-Csv C:\Users\RHyman\Documents\ExListTest.csv
FOREACH ($Person in $UserList)
{
Get-ADUser -Identity $UserList.UserPrincipalName

Use CSV file with Names to get ADUser Email Addresses

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

use samaccountnames to export user properties(mainly just first name and lastname) to csv file

I have a CSV file containing the samaccount name of some users.
From this list, I want to export the properties of these users to a CSV file.
Kindly share the simplest possible way to do so in Windows Powershell ISE.
I have tried this :
Import-ModuleActiveDirectory
Import-CSV C:\scripts\list.csv | ForEach{Get-ADUser -Identity $samaccountname-Filter*-Properties*|export-csv c:\ADusers.csv
}
Thank you!
You didn't show us the first couple of lines of the CSV file.
A proper CSV file has multiple fields and a header line like this:
"AccountName","EmailAddress"
"doe","john.doe#example.com"
"kent","clark.kent#example.com"
If this is the case, do:
Import-ModuleActiveDirectory
$userProperties = 'GivenName', 'SurName', 'Initials'
Import-Csv -Path "C:\Scripts\List.csv" | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.AccountName)'" -Properties $userProperties -ErrorAction SilentlyContinue
if ($user) {
$user | Select-Object -Property $userProperties
}
} | Export-Csv "C:\ADUsers.csv"
If the file you load only has SamAccountNames each listed on a new line, then this is not a CSV file and you should use:
Import-ModuleActiveDirectory
$userProperties = 'GivenName', 'SurName', 'Initials'
Get-Content -Path "C:\Scripts\List.csv" | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties $userProperties -ErrorAction SilentlyContinue
if ($user) {
$user | Select-Object -Property $userProperties
}
} | Export-Csv "C:\ADUsers.csv"
As you can see, I'm not using the -Identity parameter here, because in case a user with that SamAccountName is not found, an exception is thrown.
This way, output is only generated when the user actually exists.
Also, it is a bad idea to use -Properties * when you only want some of the properties returned.
Hope that helps
if you wanna do this in the ISE, you probably dont need/want to use oneliner for that.
I would suggest to import the CSV first, and then run foreach.
$list = Import-CSV -path $filePath
$result = New-Object System.Collections.ArrayList
foreach ($name in $list){
$adUser=Get-ADUser -Identity $name
$result += $adUser
}
From here, you can start thinking of error handling etc.
This will help you:
Import-ModuleActiveDirectory
Import-CSV -Path "C:\Scripts\List.csv" | Foreach {
Get-ADUser -Identity $_ -Filter * -Properties *
} | Export-CSV "C:\ADUsers.csv"
Your code was not working because $samaccountname was empty and blank not containing the username. So I replaced it with the automatic variable $_
Put each SamAccountName on its own line in the list file.
Example:
user1
user2
user3
Change list.csv to a text file (list.txt) and try this:
$username = Get-Content "C:\scripts\list.txt"
ForEach($user in $username){
Get-ADUser -Identity $user | Select GivenName,Surname,Initials | Export-CSV -Path "C:\ADUsers.csv"
}

Cannot get information from Get-ADUser to output to CSV

I have a SUPER simple query of Get-ADUser select mail that I need to output to CSV. The query fetches the information that I am looking for and prints to screen but when I attempt to use the Export-Csv OR Out-File cmdlets it creates a blank document.
$Users = Import-Csv C:\users\bob\Desktop\Administrator.csv
foreach ($User in $Users) {
$User = $User.UserName
Get-ADUser $User -Properties * |
Select mail |
Export-Csv -Path C:\Users\miker99a\Desktop\DiscoveryED.csv
}
Im a chucklehead. The answer is -append, I was overwriting the entries as the script ran
$Users = Import-Csv C:\users\bob\Desktop\Administrator.csv
foreach ($User in $Users) {
$User = $User.UserName
Get-ADUser $User -Properties * | Select mail | Export-Csv -Path C:\Users\miker99a\Desktop\DiscoveryED.csv -Append
}

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.