PowerShell Script Filtering

I am running the following code set to list domain accounts and services they currently logged into. The script is also capable of listing the machine name that the account is currently logged onto.  I need to know how to augment this script to filter out OUs that I want to exclude. For example, I want to exclude:  ABC and XYZ when the script run against my domain.


Import-Module ActiveDirectory

$Computers= (Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like "*server*"})
$ErrorLog = "\\apg1ex\users\test\ServiceAccountsError.log"
ForEach ($Computer in $Computers)
Write-Host $Computer
Get-WmiObject  -Impersonation 3 -Authentication PacketPrivacy -ComputerName $Computer.name -class Win32_Service -ErrorAction Continue 2>>$ErrorLog |
Where {$_.StartName -ne "LocalSystem"} |
Where {$_.StartName -ne "NT AUTHORITY\Network Service"} |
Where {$_.StartName -ne "NT AUTHORITY\NetworkService"} |
Where {$_.StartName -ne "NT AUTHORITY\Local Service"} |
Where {$_.StartName -ne "NT AUTHORITY\LocalService"} |
Where {$_.StartName -ne "NT AUTHORITY\SYSTEM"} |
Select StartName,Name,DisplayName

Get-Process | Out-File \\apg1ex\users\test\pairedservices.txt -width 120

Once again, I am still learning PowerShell.  Any help you can provide will be greatly appreciated.


lipotechSys EngAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Subash SundharanIT Infrastructure Architect Commented:
I guess you are trying to exclude the computer accounts from specific OU. In that case, one option is to filter based on DistinguishedName
For example..
$Computers= Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like "*server*"}  | 
	?{$_.DistinguishedName -notmatch 'OU=ABC,OU=Computers,OU=Company,DC=Domain,DC=com' `
    -and $_.DistinguishedName -notmatch 'OU=XYZ,OU=Computers,OU=Company,DC=Domain,DC=com'}

Open in new window

Refer similar question which i answered earlier..

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
lipotechSys EngAuthor Commented:
Excellent solution.  Thank you.
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.