Help with Powershell


I need help with powershell script. I want to delete contact's (in domain a) but contact that are connected to AD blocked Accounts with email adresses (AD blocked Accounts in domain b).  Thanks for your help.

Leos LeosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
do you mean contacts or users who are disabled?

are you talking two office 365 instances?

afaik, blocking is only a thing in office 365, and isn't really about blocking email, it's about blocking credential sync, is that what you're trying to accomplish?

if not, then are you trying to get all of ths exchange spam blocking lists (its one per users mailbox) ganerate a single list off of those, and then use that to delete AD accounts in another domain that match those senders?
Leos LeosAuthor Commented:
I mean contact to Accounts that are blocked in other domain. About blocked account i mean account that are turn off. And this is not the Office 365.
Leos LeosAuthor Commented:
Sorry for my english. I try explain to you other way:

We have domain a - here are blocked accounts (turn off accounts)
we have domain b - here are contacts to accounts in domain a

Now. I need script to check account blocked in domain a, than check if this account have contact in domain b - if yes delete.

Thanks for help.
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
find disabled user in domain A delete contacts with the same email address from domain B?
Leos LeosAuthor Commented:
Yes that's it.
Leos LeosAuthor Commented:
So you can help me with that?
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Hello Leos Leos, yes, I got quite sick this weekend and didn't post, even though I got on to do it, I just couldn't concentrate.

Here you are.

 On the source domain run this:

#Import-Module activedirectory
Import-Module activedirectory

# Put the path you want the CSV File to be in
$CSVFile = "C:\Admin\emails.csv"

#Get list iof disabled users which have email addresses:
$DisabledEmails=Get-ADUser -properties * -Filter '(emailaddress -ne $false) -and (enabled -eq $false)' | Select emailaddress

#Save list of disabled users to CSV to import into other domain.
$DisabledEmails | Export-CSV $CSVFile

Open in new window

Copy the CSV File to the destination domain where you want to remove the contacts and run the following code.

  As noted in the code below: Remove "-Whatif" to actually delete the contacts

#Import-Module activedirectory
Import-Module activedirectory

# Put the path you want the CSV File to be in
$CSVFile = "C:\Admin\emails.csv"

#Import listof disabled Users
$DisabledEmails2 = Import-CSV $CSVFile

#Get list of contacts whcih match the list of disabled users
$MatchedContacts = Get-ADObject -properties * -LDAPFilter "objectClass=Contact" | ? {$_.Mail -in $DisabledEMails2.emailaddress}

# Remove "-Whatif" to actually delete the contacts
$MatchedContacts | Remove-ADObject -confirm:$false -whatif

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leos LeosAuthor Commented:
Thank you very much.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Glad to help!  Once you have a chance please mark the comment as Accepted :)
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Ben Personick (Previously QCubed) (https:#a42365835)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.