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
  • 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 Jun 12 at 16:24
  • @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 Jun 12 at 16:35
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.

  • Worked like butter! Thanks for simplifying and cleaning it up for me. Now on to the second portion of the script. – Kevin Jun 12 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.