Skip to content

Instantly share code, notes, and snippets.

@joshfinley
Created September 25, 2023 18:55
Show Gist options
  • Save joshfinley/15b062ecc320d92d565ffbcf90b288d2 to your computer and use it in GitHub Desktop.
Save joshfinley/15b062ecc320d92d565ffbcf90b288d2 to your computer and use it in GitHub Desktop.
# Import the Active Directory module if not already loaded
Import-Module ActiveDirectory
# Create a script block to process the piped input
filter CheckADUser {
# Assume $_ is the object passed from the pipeline, attempt to select the Name property
$name = $_.Name
# If Name property is found, check the AD
if ($null -ne $name) {
# Attempt to get the user from AD
$user = Get-ADUser -Filter { Name -eq $name } -ErrorAction SilentlyContinue
# If user is found, check if it's a human user ID based on some criteria (e.g., DistinguishedName does not contain 'ServiceAccounts')
if ($null -ne $user) {
if ($user.DistinguishedName -notmatch 'ServiceAccounts') {
$user
}
}
}
}
# Example Usage:
# YourPreviousCommand | Format-List | CheckADUser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment