Condition blocks not working

Hi All,
I am using the following script to check recipients and types and add aliases from the feed file.

#Get the name of the file that will be added to Domain Reports Folder
$filename = Read-Host "Enter the domain name"
# Filepath of the feed file for specific domain
$filepath = Read-Host "Enter the feed file path"
$filepath = $filepath.Replace('"','') # Removes quotes when pasting the path

# Import CSV and process each object
import-Csv $filepath | ForEach-Object {
    $recipient = Get-Recipient $_.Email
    $output = New-Object PSObject -Property @{
        Alias = $_.Alias
        Email = $_.Email
        PrimarySmtpAddress = $recipient.PrimarySmtpAddress
        RecipientTypeDetails = $recipient.RecipientTypeDetails
    }
    $output

    # Depending on the RecipientTypeDetails, perform the appropriate action
       if ($_.RecipientTypeDetails -eq "MailUniversalDistributionGroup") {
        Set-DistributionGroup $_.Email -EmailAddresses @{add=$_.Alias}
    }
    else {
        Set-RemoteMailbox $_.Email -EmailAddresses @{add=$_.Alias}
    }
} 2>> "C:\Users\Documents\Projects\Domain Map Decom\Domain Reports\$filename`_AliasErrors.txt" | Export-Csv "C:\Users\Documents\Projects\Domain Map Decom\Domain Reports\$filename`_AliasChecked.csv" -NTI 2>> "C:\Users\Documents\Projects\Domain Map Decom\Domain Reports\AliasAddErrors.txt"

I’m having a hard time understanding why the if condition is ignored and goes straight to the else condition. Any guidance is appreciated.

Thanks in advance!

How does your input CSV look like?
… or to be more precise … does your input CSV have a column with the header RecipientTypeDetails?

It doesn’t, the $output variable does and I had thought I scripted it to be used with the condition statements.

Just change the $_ in your condition to $Output and it should work.

… well … no … since you use $_.RecipientTypeDetails instead of $output.RecipientTypeDetails you’re checking the non existent property RecipientTypeDetails of your pipeline variable $_. :man_shrugging:t3:

So Courtney’s (@grey0ut) tipp should solve your issue. :point_up:t3: :wink:

Thank you both so much, that worked, and I see the mistake I made now.