Active Directory (AD) management is an essential aspect of system administration, ensuring the security and efficiency of network operations. Today, I’m sharing a PowerShell script that simplifies the task of retrieving crucial information about service accounts in AD.

# Script Type: AD Service Account Info - Service Account Info.ps1
# Author: Wesley Ellis
# Date: November 23, 2020
# Description: Retrieves information about a specified service account in Active Directory.

# Define credentials
$username = "<username>"
$password = "<password>"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)

# Define domain controller
$domainController = "<domain controller name>"

# Define service account name
$serviceAccount = "<service account name>"

# Retrieve information about the service account from Active Directory
Invoke-Command -ComputerName $domainController -Credential $credential -ScriptBlock {
    Import-Module ActiveDirectory
    $serviceAccount = $args[0]
    $serviceAccountDN = (Get-ADServiceAccount -Identity $serviceAccount).DistinguishedName
    $domainName = $serviceAccountDN.split(",")[1] -replace "^DC=",""
    [PSCustomObject]@{
        Service Account = $serviceAccount
        Domain Name = $domainName
    }
} -ArgumentList $serviceAccount

This script offers a straightforward way to access and display service account information, streamlining AD management tasks and enhancing network security.