Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $searchfilter = {
- (EmailAddress -like "*") -or
- (extensionAttribute14 -like "*") -or
- (extensionAttribute15 -like "*") -or
- (mail -like "*") -or
- (msExchShadowProxyAddresses -like "*") -or
- (msRTCSIP-PrimaryUserAddress -like "*") -or
- (personalPager -like "*") -or
- (proxyAddresses -like "*") -or
- (targetAddress -like "*") -or
- (UserPrincipalName -like "*")
- }
- $properties = "SamAccountName","EmailAddress","extensionAttribute14","extensionAttribute15","mail","msExchShadowProxyAddresses","msRTCSIP-PrimaryUserAddress","personalPager","proxyAddresses","targetAddress","UserPrincipalName"
- $adusersemails = Get-ADUser -Filter $searchfilter -Properties $properties | select $properties
- $results = @()
- foreach ($user in $adusersemails) {
- foreach ($singleproperty in $properties) {
- if ($user.($singleproperty) -notlike "") { #iterate through the properties that only have data
- if($user.($singleproperty).GetType().Name -eq "ADPropertyValueCollection") { #if it is a property value collection
- foreach ($collectionitem in $user.($singleproperty)) { #foreach item in the collection
- if ($collectionitem.Contains("@")) { #if it contains a whir
- $results += New-Object -TypeName PSObject -Property @{"samaccountname"=$user.SamAccountName;"emailaddress"=$collectionitem.ToLower().Replace("smtp:","").Replace("sip:","")}
- } #if it contains a whirl
- } #foreachitem in the collection
- } else {
- if ($user.($singleproperty).Contains("@")) { #if it contains a whir
- $results += New-Object -TypeName PSObject -Property @{"samaccountname"=$user.SamAccountName;"emailaddress"=$user.($singleproperty).ToLower().Replace("smtp:","").Replace("sip:","")}
- } #if it contains a whirl
- } #if else collection or string
- } #if the property has datea
- } #foreach property
- } #foreach user
- $results = $results | Sort-Object -Property emailaddress | Get-Unique -AsString
- $results
Advertisement
Add Comment
Please, Sign In to add comment