How to sort email into two different lists using powershell

0

In our organization we have users who are setup to have email sent to domains outside of our organization (Mail Users). So their primary email address is different from our standard FQDN.

What I am trying to do is pull all enabled users in AD and sort the users with our primary FQDN into one last and users with anything else into another list.

I have been able to get the following to produce a single list but I cannot get it to sort it into two lists by email type.

$users = Get-ADUser -Filter {Enabled -eq $true} -SearchBase "Removed" -Properties mail, givenName, initials, sn, title,  extensionAttribute3, extensionAttribute4, physicalDeliveryOfficeName, department, extensionAttribute1, employeeid, EmployeeType, whenCreated | where-object {$_.EmployeeType -ne $null -AND $_.mail -ne $null} | 
foreach {
        new-object psobject -Property @{
                                         Email = $_.mail
                                         firstName = $_.givenName
                                         lastName = $_.sn
                                         middleInitial = $_.initials
                                         jobTitle = $_.title
                                         Department = $_.department
                                         UserID = $_.employeeid
                                         }
       } | Select Email,firstName,lastName,middleInitial,jobTitle,Department,UserID


$goodEmail = @()
$badEmail = @()


foreach ($user in $users) {
    if($users.'Email' -ne "*@example.com") {

    $goodEmail += $user

}
else {

    $badEmail += $user
}
}

$goodEmail | Export-CSV $csvOriginal -NoTypeInformation -Append
$badEmail | Export-CSV $badEmails -NoTypeInformation -Append

Kevin

Posted 2019-06-12T15:14:17.187

Reputation: 1

Hi, I would try changing - if($users.'Email' -ne "@example.com") to if($user.Email -notlike "@example.com"). From memory I dont think you can use -eq/-ne with wildcards. You are also using the array and not the single object when comparing the data. – CraftyB – 2019-06-12T16:24:32.823

@CraftyB This is just my most recent iteration. I forgot to mention, obviously, I have tried -like, -notlike, and the current form and every version continues to dump into the single threaded file. – Kevin – 2019-06-12T16:35:34.093

Answers

0

I'd simplify the script by

  • putting the needed properties into an array (there are some unused)
  • using a [PSCustomObject] for output (keeps order by default)
  • use a Where-Object to distinguish between good/bad email

## Q:\Test\2019\06\12\SU_1447905.ps1
$Props = ('mail','givenName','initials','sn','title','physicalDeliveryOfficeName',
          'extensionAttribute1','extensionAttribute3','extensionAttribute4',
          'department','employeeid','EmployeeType','whenCreated')

$users = Get-ADUser -Filter {Enabled -eq $true} -SearchBase "Removed" -Properties $Props | 
    Where-object {$_.EmployeeType -ne $null -AND $_.mail -ne $null} | 
        ForEach-Object {
            [PSCustomObject]@{
                Email         = $_.mail
                firstName     = $_.givenName
                lastName      = $_.sn
                middleInitial = $_.initials
                jobTitle      = $_.title
                Department    = $_.department
                UserID        = $_.employeeid
            }
       }

$goodEmail = $users | Where-Object Email -like    '*@Example.com'
$badEmail  = $users | Where-Object Email -notlike '*@Example.com'

$goodEmail | Export-CSV $csvOriginal -NoTypeInformation -Append
$badEmail  | Export-CSV $badEmails   -NoTypeInformation -Append

$csvOriginal and $badEmails are defined outside of this script.

LotPings

Posted 2019-06-12T15:14:17.187

Reputation: 6 150

Worked like butter! Thanks for simplifying and cleaning it up for me. Now on to the second portion of the script. – Kevin – 2019-06-12T17:42:27.767