I am trying to complete a script that will query the domain, but exclude some OUs and accounts that start with specific items, such as "SystemMailbox", or "_".
I started this but I am wondering if there is a better method to doing this, and also I am not sure of the best way to send this email. A csv file is fine. The last item I am a bit stuck on is the group that made this request wants to have # of days since last logon, so I am pulling last logon date, but I am unsure of how to calculate that within the script?
Get-ADUser -Filter * -Properties DisplayName, sAMAccountName, EmployeeType, EmployeeID, LastLogonDate,whenCreated,Enabled -SearchBase "DC=mydomain,DC=local" |
? {$_.Name -notlike "*SystemMailbox*" `
-and $_.SamAccountName -notlike "*_*" `
-and $_.SamAccountName -notlike "*System Attendant*" `
-and $_.SamAccountName -notlike "*SMTP*" `
-and $_.SamAccountName -notlike "*krbtgt*"
-and $_.DistinguishedName -notmatch 'CN=SharedMailboxes,DC=yourdomain,DC=com' `
-and $_.DistinguishedName -notmatch 'CN=ServiceAccounts,DC=yourdomain,DC=com'} |
Select Name,SamAccountName,Title,Department,LastLogonDate,whenCreated,Enabled |
Export-Csv "C:\myscripts\ADusers.csv" –NoTypeInformation