Trying to Filter all about 3 email address in my organisation using a csv file, cant get it to work

Hi, Hoping for the some help. Im a newbie and trying to write my own scripts.

I’ve written a CSV which im importing into my powershell see below:

image

I need to pull all contact objects for 3 different domainnames and export to CSV but the below keeps pulling into the csv all contacts in the organisation

foreach ($Domain in (import-csv "C:\Temp\MailDomain.CSV")) 
{
$emailaddress = $Domain.Name
Get-MailContact -Filter {WindowsEmailAddress -like $emailaddress} | Select-Object -Property ExternalEmailAddress,Alias,PrimarySmtpAddress,RecipientType,WindowsEmailAddress | Sort-Object Name | Export-Csv c:\contacts.csv
}

Any help would be appreciated, I know my main command is correct, the bit below as it returns what i need for an individual domain, but not for the multiple which ive specified on the importing CSV file.

Get-MailContact -Filter {WindowsEmailAddress -like “@domainame1.co.uk”} | Select-Object -Property ExternalEmailAddress,Alias,PrimarySmtpAddress,RecipientType,WindowsEmailAddress | Sort-Object Name | Export-Csv c:\contacts.csv

The csv would be overwritten for each iteration of the loop, so you would only see the last domain query. Try something like this:

$results = foreach ($Domain in (import-csv "C:\Temp\MailDomain.CSV")) {
    $emailaddress = $Domain.Name
    Get-MailContact -Filter {WindowsEmailAddress -like "*$emailaddress"} | 
        Select-Object -Property ExternalEmailAddress,Alias,PrimarySmtpAddress,RecipientType,WindowsEmailAddress | 
            Sort-Object Name 
}

$results | Export-Csv c:\contacts.csv

Also, please use the code tags (</> in toolbar) to format the code

1 Like

@rob-simmers, thanks for the assistance, i’ve just tried that and I now just receive an empty contacts.csv with no data, where previous I had data in there, but just not the data I was trying to filter. Just can’t think what is wrong - Like i say the command it self works individually, but its the way im using the Import-csv

The quotes around the path in the pasted code looked incorrect (most likely because code tags were not used), so I’ll correct them, but this is basically the same concept for reference:

$csv = @"
Name
vmi
VMWare
"@ | ConvertFrom-Csv

$results = foreach ($service in ($csv)) {
    $service = $service.Name
    Get-Service -Name "$service*"
}

Hi Rob-simmers, was you above reply meant for me, as it makes no sense to me.

Yes, it was meant for you. The Import-Csv may have not been working because the qoutes were messed up from when the code was copied\pasted into the forums, I so I updated the previous post. Additionally, I was just providing a basic example using Get-Service to show you that the base code does work.

ok thanks. I have now read over you post a few times. So I see in your last post your suggesting to change the way I bring the multiple domain names. So using your method with the services I drafted the below. However I still receive a blank .CSV for my output…

This is actually the first time im trying to bring in multiple values for which I want my command to run against - So apologise for being such a Novice.

However using your method I have put together the below, but still get a blank CSV. Is the below correct? Using your method

$csv = @"
Name
domainname1
domainname2
domainname3
"@ | ConvertFrom-Csv

$results = foreach ($Domains in ($csv)) {
    $Domains = $Domains.Name
    Get-MailContact -Filter {WindowsEmailAddress -like "*$Domains"} | 
        Select-Object -Property ExternalEmailAddress,Alias,PrimarySmtpAddress,RecipientType,WindowsEmailAddress | 
            Sort-Object Name 
}

$results | Export-Csv c:\contacts.csv

Let’s take a step back. Filters can be a bit finicky and you are trying to resolve a variable and create a wildcard search with -LIKE:

$domainName = 'domain1.com'

# try this
$results = Get-MailContact -Filter {WindowsEmailAddress -like "*$domainName"}
# or this
#$results = Get-MailContact -Filter {WindowsEmailAddress -like ('*{0}' -f $domainName)}

$results

Are you getting results with the query formatted with either of those?

@rob-simmers ive tried that. And see in the pic below. The $domainname variable configures correctly. However I recieved errors for the second line you gave me for the $Results variable. And the i try you 1st line for th $results variable, i have no output (eg: when i try to display the variable content by typing it, it simply goes back to the C:\ command prompt seens on the picture.

I would try

-Filter "property -like '*$value'"

With double quotes instead of braces and the value portion surrounded by single quotes

Couple of issues with your original attempt.

  1. In your CSV, the column name is domain - not Name.
  2. You select 5 properties, but then attempt to sort on Name which is not one of the five.

Your code should be as simple as this.

Import-Csv -Path "C:\Temp\MailDomain.CSV" | ForEach-Object {
    Get-MailContact -Filter "windowsemailaddress -like '*$($_.domain)'" |
        Select-Object -Property ExternalEmailAddress,Alias,PrimarySmtpAddress,RecipientType,WindowsEmailAddress
} | Export-Csv -Path c:\contacts.csv -NoTypeInformation

Thanks all for the support it is appreciated - This last post worked for me