Script Help - AD User Report

Howdy

I’m trying to use this script I found online and I am stuck trying to modify it.

I have to remove some of the items this pulls, but I can do that. I need to query the entire domain, exclude specific OUs, and then exclude account names that start with specific characters or strings like “service”. Then setup it up as a scheduled task and have it run and send an email out.

I’m stuck and I’m just new so after many hours I’m hoping someone can help?

{
$path = Split-Path -parent “$CSVReportPath*.*”
$pathexist = Test-Path -Path $path
If ($pathexist -eq $false)
{New-Item -type directory -Path $path}

$reportdate = Get-Date -Format ssddmmyyyy 

$csvreportfile = $path + "\ALLADUsers_$reportdate.csv" 
 
#import the ActiveDirectory Module 
Import-Module ActiveDirectory 
 
#Perform AD search. The quotes "" used in $SearchLoc is essential 
#Without it, Export-ADUsers returuned error 
              Get-ADUser -server $ADServer -searchbase "$SearchLoc" -Properties * -Filter * |  
              Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
              @{Label = "Last Name";Expression = {$_.Surname}}, 
              @{Label = "Display Name";Expression = {$_.DisplayName}}, 
              @{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
              @{Label = "Full address";Expression = {$_.StreetAddress}}, 
              @{Label = "City";Expression = {$_.City}}, 
              @{Label = "State";Expression = {$_.st}}, 
              @{Label = "Post Code";Expression = {$_.PostalCode}}, 
              @{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')  ) {'United Kingdom'} Else {''}}}, 
              @{Label = "Job Title";Expression = {$_.Title}}, 
              @{Label = "Company";Expression = {$_.Company}}, 
              @{Label = "Description";Expression = {$_.Description}}, 
              @{Label = "Department";Expression = {$_.Department}}, 
              @{Label = "Office";Expression = {$_.OfficeName}}, 
              @{Label = "Phone";Expression = {$_.telephoneNumber}}, 
              @{Label = "Email";Expression = {$_.Mail}}, 
              @{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}}, 
              @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled 
              @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} |  
               
              #Export CSV report 
              Export-Csv -Path $csvreportfile -NoTypeInformation     

}

If you’re new or beginner you might start with something easier than to modify code you don’t understand. Try to split the task you have to accomplish in smaller parts you can deal with and ‘connect’ the results later.

rather than rewrite that script for you i find its better to learn when someone shows me the query / part i need then i figure out where to put it

#######################
####Set OU Location####
#######################

$OU = "OU=test,DC=test,DC=test,DC=com"

#######################################
####Get Users matching querys below####
#######################################

Get-AdUser -Searchbase $OU -Filter * -Properties canonicalname |  ? { 

     $_.samaccountname -notlike "*service*" -and
     $_.canonicalname -notlike "*Disabled*"
     
     } 

The above will give you all users in a domain or OU (provided you modify the $OU = line) that do not have a name that contains the word “Service” and excludes OU’s that contain the work “disabled” (modify that to suit your needs) if you need more criteria just add more scopes

$_.samaccountname -notlike "*service*" -and
$_.canonicalname -notlike "*Disabled*" -and
$_.canonicalname -notlike "*Dib*" -and
$_.canonicalname -notlike "*Dab*"