How to retrieve managed service accounts in Active Directory using Get-ADServiceAccount

Retrieving Active Directory service accounts

Managing Active Directory (AD) service accounts is essential for securing automated processes, applications, and services that require domain authentication. IT admins often need to retrieve details of these accounts to audit permissions, monitor usage, and ensure compliance with security policies. While the Get-ADServiceAccount PowerShell command allows admins to fetch information about managed service accounts, it often requires extensive scripting.

Get AD service accounts in PowerShell

Prerequisites

Before running the Get-ADServiceAccount cmdlet, ensure the following requirements are met:

  • The AD module for Windows PowerShell is installed. If it's not installed, run:
    Install-WindowsFeature -Name RSAT-AD-PowerShell
  • The AD module is loaded. Import the AD module if it's not loaded:
    Import-Module ActiveDirectory

Using the Get-ADServiceAccount cmdlet to retrieve AD service accounts

Use the Get-ADServiceAccount cmdlet to retrieve managed service accounts. The syntax is as follows:

Get-ADServiceAccount
[-AuthType <ADAuthType>]
[-Credential <PSCredential>]
[-Identity] <ADServiceAccount>
[-Partition <String>]
[-Properties <String[ ]>]
[-Server <String>]
[<CommonParameters>]

Example use cases and scripts of using Get-ADServiceAccount

Example 1: Get a filtered list of managed service accounts

Get-ADServiceAccount -Filter "HostComputers -eq 'CN=SQL-Server-1, DC=contoso,DC=com'"
Enabled : True
Name : service1
UserPrincipalName :
SamAccountName : service1$
ObjectClass : msDS-ManagedServiceAccount
SID : S-1-5-21-159507390-2980359153-3438059098-29770
ObjectGUID : eaa435ee-6ebc-44dd-b4b6-dc1bb5bcd23a
HostComputers : {CN=SQL-Server-1, DC=contoso,DC=com}
DistinguishedName : CN=service1,CN=Managed Service Accounts,DC=contoso,DC=com

Example 2: Get a managed service account using the Security Account Manager name

Get-ADServiceAccount -Identity service1
Enabled : True
Name : service1
UserPrincipalName :
SamAccountName : service1$
ObjectClass : msDS-ManagedServiceAccount
SID : S-1-5-21-159507390-2980359153-3438059098-29770
ObjectGUID : eaa435ee-6ebc-44dd-b4b6-dc1bb5bcd23a
HostComputers :
DistinguishedName : CN=service1,CN=Managed Service Accounts,DC=contoso,DC=com

Supported parameters

The table below lists key parameters that can be used with the Get-ADServiceAccounts cmdlet to get AD service accounts efficiently.

Parameters Description
-AuthType This specifies the authentication method to use.
-Credential This specifies the user account credential to use to perform this task.
-Filter This specifies a query string that retrieves AD objects.
Identity This specifies an AD account object by giving one of the following property values:
  • A distinguished name
  • A SAM account name
  • A GUID
  • A security identifier

Challenges of using PowerShell scripts to retrieve AD service accounts

  • PowerShell has limited error handling and debugging, making complex queries harder to troubleshoot. Additionally, processing large datasets can be slow.
  • Efficient use of PowerShell requires advanced scripting skills and proper module setup, increasing the risk of errors.
  • Delegation can be challenging since technicians require elevated permissions.

Why use ADManager Plus for management and reporting

Manage AD objects in bulk with ADManager Plus

 
  • Retrieving Active Directory service accounts
  • Get Active Directory service accounts PowerShell
  • Challenges of using PowerShell scripts to retrieve Active Directory service accounts
  • Why use ADManager Plus for management and reporting
The one-stop solution to Active Directory Management and Reporting
Email Download Link Email the ADManager Plus download link