Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Script to get usernames based on email address

$
0
0

Hello Everyone,

We are having an issue with starting to clean up Active Directory in preparation for Azure AD Connect deployment. There are a number of user accounts that we do not need to sync (16,000), and we need to add a flag to them to not sync them. We have a list of email addresses that are linked to user accounts, but we don't have the user names. We could do and manually find these, but that's not realistic. The duplicated addresses were exported from IDfix, they have an associated contact that we still need. The user accounts those email addresses are listed on do not need to be synced. They are all different addresses, from yahoo, gmail. various other external addresses.

The goal would be to take the list of email addresses that we have, run a query to get the username on them, export and append to a csv, so we can run a different script to apply and AdminDiscription attribute change to prevent a sync. 

Here is what I have, 

$exportlocation = c:\sripts\useracct.csv
$importlocation = c:\scripts\emailList.csv

foreach ($line in $importlocation) {Get-ADUser -Filter {mail -eq $line.email}}  | Export-csv -Path $exportlocation -append

I keep getting an empty pipe error. I can run the get-aduser part no problem on its own, it just doesn't return the UPN or simply SamAccountName on its own to export. When I add the Export-csv to the standalone script with manual email address entry I return:

 Export-csv : Cannot append CSV content to the following file: C:\scripts\useracct.csv. The appended object does n
have a property that corresponds to the following column: . To proceed with mismatched properties, add the -Force
switch and retry.

Adding -Force simply puts the attributes in the csv on one line with nothing else.  Little stumped. I don't care if it exports everything as long as its in separate columns that I can copy and paste, but I'm as about as far as I know right now.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>