I have a set of files that represent export of Active Directory security groups members. These files contain user email addresses. I would like to use PowerShell to scan all the files (~300) and by using Get-ADUser cmdlet find user account names based on the email addresses stored in these files and then save the output in new files in another folder.
I could of course do a different AD export and fetch user account names instead of email addresses, but it wouldn't be helpful in this case, because I'm working on porting user access permissions from one AD domain to another AD domain (without any trust between the two) and the only thing that was done to help me is that in the old domain the user accounts were modified to contain email addresses from the new domain, hence the email addresses match in both old and new domain, and as I now have a text file per AD group with email addresses in them matching the new domain, I can use these addresses to fetch users' new account names from the new domain.
So far I was able to list the files and do the email to account name mapping using the following code:
$directory = 'c:\temp\groups\all'
$files = Get-ChildItem -Path $directory -File *.txt -Recurse | Select -expand fullname
ForEach ($file in $files)
{
Get-Content $file |ForEach-Object
{Get-ADUser -Filter {mail -like $_} -properties mail | Select-Object SamAccountName}
}
However, right now I'm stuck trying to figure out how to output the changes back into text files that would have the same name as originals, but be placed in a different folder.
I'm sure the code above can be made better; please bear with me, I'm beginner.