# Import the Active Directory module if it's not already loaded
if (-not (Get-Module -Name ActiveDirectory)) {
Import-Module ActiveDirectory
}
# Specify the SamAccountName of the user/service account
$userName = "YourUserName"
# Try to get the user/service account
$user = Get-ADUser -Filter { SamAccountName -eq $userName } -ErrorAction SilentlyContinue
# Check if the user/service account exists
if ($user) {
Write-Host "The user/service account $userName exists."
} else {
Write-Host "The user/service account $userName does not exist."
}
Groups
# Import the Active Directory module if it's not already loaded
if (-not (Get-Module -Name ActiveDirectory)) {
Import-Module ActiveDirectory
}
# Specify the name of the Active Directory group
$groupName = "YourGroupName"
# Try to get the group
$group = Get-ADGroup -Filter { Name -eq $groupName } -ErrorAction SilentlyContinue
# Check if the group exists
if ($group) {
Write-Host "The Active Directory group $groupName exists."
} else {
Write-Host "The Active Directory group $groupName does not exist."
}