Share via


Get multiple AD users based on UserPrincipalName and then export them into csv file

Question

Saturday, December 3, 2016 1:02 PM

Hi,

I have list of more than 100 people with csv file and UserPrincipalName is the having some value. for all of them i need to extract their name,email,mobile no,email etc.. from Active directory by comparing UserPrincipalName . The Output required to be in excel file.

Please suggest to be achieve this task and let me know if you have any scripts.

Subhash

All replies (6)

Monday, December 5, 2016 1:15 PM ✅Answered

Try the below code:
You need type the additional property name which you want.

Import-Csv D:\PSTest\Users.csv | Foreach {
    Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" -Properties MobilePhone,Mobile,Office
} | Select Name,GivenName,MobilePhone,Mobile,Office |
    Export-Csv D:\PSTest\Userfound.csv

Dont't use -Properties *
Type the property name which want
Ex: -Properties MobilePhone,Mobile,Office


Saturday, December 3, 2016 1:15 PM

I suggest for you to use the search either here in the forum or in Google. Something like that is asked at least twice a week. I suggest further for you to learn Powershell - at least the basics. Then you could do this easily in 2 Minutes. 

If you're looking for prewritten scripts you can find some there: Microsoft Technet Script Gallery

If you already have some code to show - show it. Usually someone here is able to help you with any issue. You just have to show a little effort you did

BTW: Some great sources to start with you can find here: Beginner Sites And Tutorials

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Sunday, December 4, 2016 2:24 PM

I suggest for you to use the search either here in the forum or in Google. Something like that is asked at least twice a week. I suggest further for you to learn Powershell - at least the basics. Then you could do this easily in 2 Minutes. 

If you're looking for prewritten scripts you can find some there: Microsoft Technet Script Gallery

If you already have some code to show - show it. Usually someone here is able to help you with any issue. You just have to show a little effort you did

BTW: Some great sources to start with you can find here: Beginner Sites And Tutorials

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''

Let me know if you have any scripts

Subhash


Sunday, December 4, 2016 2:34 PM

Did you even open any link posted by  BOfH_666

Search here for Scripts

https://gallery.technet.microsoft.com/scriptcenter/site/search

or Request for Scripts here

https://gallery.technet.microsoft.com/scriptcenter/site/requests

And also read this:

This forum is for scripting questions rather than script requests


Sunday, December 4, 2016 3:32 PM

Let me know if you have any scripts

Subhash

Do you like to do other peoples work for nothing? If the answer is 'No' you know exactly how we feel about your 'request'. We all are pleased to help others if we see that someone spend at least a little effort. You did not even used the help we already offered to you.

So to answer your last post: No - I won't.

Have a nice Weekend!

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Monday, December 5, 2016 12:59 PM

Let me know if you have any scripts

Subhash

Do you like to do other peoples work for nothing? If the answer is 'No' you know exactly how we feel about your 'request'. We all are pleased to help others if we see that someone spend at least a little effort. You did not even used the help we already offered to you.

So to answer your last post: No - I won't.

Have a nice Weekend!

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''

Below is the script which check user from csv file with AD. then extract all the attributes info then export into csv file. Thanks all for your inputs

param ($inputfile='.\Users.csv',$logfile='.\log.csv')
$csv = Import-CSV $inputfile
"samAccountName,Search Result" | Add-Content $logfile
$FinalCSV = "Results.CSV"
$outputReport = "D:\support\Results.csv"
$UserDataCollection = @()
ForEach ($user in $csv)

{
#Write-Host $user
$search = Get-ADObject -LDAPFilter cn=$($user.samAccountName)

IF ($search) # if not empty or null

{
#"$user.samAccountName, Found" | Add-Content $logfile
#Write-Host $user.samAccountName
$usr = $user.samAccountName
Write-Host $usr

#Get-ADUser -Identity "$usr" -Properties *| Select -Property Name , Mail 

Write-Host "Getting req info"

$Name = Get-ADUser -Identity "$usr" -Properties *| Select Name
$Mail = Get-ADUser -Identity "$usr" -Properties *| Select Mail
$Title = Get-ADUser -Identity "$usr" -Properties *| Select Title
$Department = Get-ADUser -Identity "$usr" -Properties *| Select Department
$OfficePhone = Get-ADUser -Identity "$usr" -Properties *| Select OfficePhone
$DisplayName = Get-ADUser -Identity "$usr" -Properties *| Select DisplayName
$Company = Get-ADUser -Identity "$usr" -Properties *| Select Company
$EmailAddress = Get-ADUser -Identity "$usr" -Properties *| Select EmailAddress
$EmployeeID = Get-ADUser -Identity "$usr" -Properties *| Select EmployeeID
$MobilePhone = Get-ADUser -Identity "$usr" -Properties *| Select MobilePhone
$mobile = Get-ADUser -Identity "$usr" -Properties *| Select mobile
$Office = Get-ADUser -Identity "$usr" -Properties *| Select Office
$physicalDeliveryOfficeName = Get-ADUser -Identity "$usr" -Properties *| Select physicalDeliveryOfficeName
$PostalCode = Get-ADUser -Identity "$usr" -Properties *| Select PostalCode
$StreetAddress = Get-ADUser -Identity "$usr" -Properties *| Select StreetAddress
$GivenName = Get-ADUser -Identity "$usr" -Properties *| Select GivenName
$Surname = Get-ADUser -Identity "$usr" -Properties *| Select Surname
$telephoneNumber = Get-ADUser -Identity "$usr" -Properties *| Select telephoneNumber

#Array  to export Data
$Output =New-Object -TypeName PSObject -Property @{
        SSO = $Name
        Email = $Mail 
        Designation = $Title
        Department = $Department
        OfficePhone = $OfficePhone
        DisplayName = $DisplayName
        Company = $Company
        EmailAddress = $EmailAddress
        EmployeeID = $EmployeeID
        MobilePhone = $MobilePhone
        mobile = $mobile        
        Office = $Office
        PostalCode = $PostalCode
        OfficeName = $physicalDeliveryOfficeName
        StreetAddress = $StreetAddress
        GivenName = $GivenName
        Surname = $Surname
        telephoneNumber = $telephoneNumber
        } | Select-Object SSO,Email,Designation,Department,OfficePhone,DisplayName,Company,EmailAddress,EmployeeID,MobilePhone,mobile,Office,PostalCode,OfficeName,StreetAddress,GivenName,Surname,telephoneNumber

 $UserDataCollection += $Output 

}

ELSE

{
"$user.samAccountName, Not Found" | Add-Content $logfile | Out-file D:\support\log.txt -Append
}

}
#do final export of data
 $UserDataCollection | Export-Csv -LiteralPath $OutputReport -NoTypeInformation

Subhash