Gotta Catch ‘Em All: Find Every Service and Scheduled Task Using an Account with PowerShell

Note: This post was originally written on October 15, 2016, and updated on November 25, 2026, to replace deprecated Get-WmiObject calls with Get-CimInstance and WinRM-based scheduled task queries.

Best practice is to use a Group Managed Service Account (gMSA) for scheduled tasks and Windows services so you never have to rotate passwords by hand. Sometimes a legacy app does not support gMSAs and you have to create a dedicated service account, or you inherit an environment where the previous admin ran everything under the Domain Admin account.

Either way, when it is time to rotate that password, you need to find every service and scheduled task using it first. If you miss one, those jobs start failing and the account locks out every five minutes. Logging into every server one by one to check services.msc and Task Scheduler is a waste of an afternoon, so let PowerShell sweep Active Directory and dump every match into two CSV files.

Prerequisites

Before you run the script, make sure you have:

  • The ActiveDirectory PowerShell module installed (part of Remote Server Administration Tools, or RSAT, or run directly on a management server).
  • An account with permissions to query remote servers over WinRM (typically a Domain Admin or delegated server admin).
  • WinRM enabled on your domain servers (enabled by default on modern Windows Server releases).

Building the Script

First, import the Active Directory module and ask for the account name you are hunting down. Include the domain prefix (DOMAIN\username), because that is how Windows services and scheduled tasks store the logon identity.

Import-Module ActiveDirectory

$TargetUser = Read-Host "Enter the account to search for (DOMAIN\username)"
$Services = [System.Collections.Generic.List[object]]::new()
$Tasks = [System.Collections.Generic.List[object]]::new()
Code language: PowerShell (powershell)

Next, query Active Directory for enabled computer accounts and check whether each machine is online before querying it. The original version of this script used Get-WmiObject and schtasks.exe, which relied on legacy DCOM/RPC. Swapping to Get-CimInstance for services and Invoke-Command with Get-ScheduledTask for scheduled tasks works cleanly in both Windows PowerShell 5.1 and PowerShell 7+ over WinRM.

Tip: If your environment has thousands of workstations, narrow -Filter or add -SearchBase to target only your Servers OU so you are not pinging every laptop on the network.

$Computers = Get-ADComputer -Filter "Enabled -eq 'True'" -Properties DNSHostName

foreach ($Computer in $Computers) {
    $HostName = $Computer.DNSHostName
    if (-not $HostName) { continue }

    if (Test-Connection -ComputerName $HostName -Count 1 -Quiet) {
        try {
            $MatchedServices = Get-CimInstance -ClassName Win32_Service `
                -ComputerName $HostName `
                -ErrorAction Stop |
                Where-Object { $_.StartName -eq $TargetUser } |
                Select-Object @{N='ComputerName';E={$_.SystemName}},
                    DisplayName, Name, State, StartMode, StartName

            foreach ($Svc in $MatchedServices) { $Services.Add($Svc) }

            $MatchedTasks = Invoke-Command -ComputerName $HostName `
                -ErrorAction Stop `
                -ScriptBlock {
                param($User)
                Get-ScheduledTask |
                    Where-Object { $_.Principal.UserId -eq $User } |
                    ForEach-Object {
                        [PSCustomObject]@{
                            ComputerName = $env:COMPUTERNAME
                            TaskName     = $_.TaskName
                            TaskPath     = $_.TaskPath
                            State        = $_.State
                            RunAsUser    = $_.Principal.UserId
                        }
                    }
            } -ArgumentList $TargetUser

            foreach ($Task in $MatchedTasks) { $Tasks.Add($Task) }
        }
        catch {
            Write-Warning "Failed to query $HostName`: $($_.Exception.Message)"
        }
    }
}
Code language: PowerShell (powershell)

Finally, export both collections to CSV files on your Desktop so you have a clean checklist before you rotate the password.

$Desktop = [Environment]::GetFolderPath('Desktop')
$Services |
    Export-Csv -Path (Join-Path $Desktop 'Services.csv') -NoTypeInformation
$Tasks |
    Export-Csv -Path (Join-Path $Desktop 'ScheduledTasks.csv') -NoTypeInformation

Write-Host "Done. Found $($Services.Count) services and $($Tasks.Count) scheduled tasks."
Code language: PowerShell (powershell)

Full Script

Here is the complete script in one block so you can save it straight to Find-ServiceTaskLogon.ps1:

Import-Module ActiveDirectory

$TargetUser = Read-Host "Enter the account to search for (DOMAIN\username)"
$Services = [System.Collections.Generic.List[object]]::new()
$Tasks = [System.Collections.Generic.List[object]]::new()

$Computers = Get-ADComputer -Filter "Enabled -eq 'True'" -Properties DNSHostName

foreach ($Computer in $Computers) {
    $HostName = $Computer.DNSHostName
    if (-not $HostName) { continue }

    if (Test-Connection -ComputerName $HostName -Count 1 -Quiet) {
        try {
            $MatchedServices = Get-CimInstance -ClassName Win32_Service `
                -ComputerName $HostName `
                -ErrorAction Stop |
                Where-Object { $_.StartName -eq $TargetUser } |
                Select-Object @{N='ComputerName';E={$_.SystemName}},
                    DisplayName, Name, State, StartMode, StartName

            foreach ($Svc in $MatchedServices) { $Services.Add($Svc) }

            $MatchedTasks = Invoke-Command -ComputerName $HostName `
                -ErrorAction Stop `
                -ScriptBlock {
                param($User)
                Get-ScheduledTask |
                    Where-Object { $_.Principal.UserId -eq $User } |
                    ForEach-Object {
                        [PSCustomObject]@{
                            ComputerName = $env:COMPUTERNAME
                            TaskName     = $_.TaskName
                            TaskPath     = $_.TaskPath
                            State        = $_.State
                            RunAsUser    = $_.Principal.UserId
                        }
                    }
            } -ArgumentList $TargetUser

            foreach ($Task in $MatchedTasks) { $Tasks.Add($Task) }
        }
        catch {
            Write-Warning "Failed to query $HostName`: $($_.Exception.Message)"
        }
    }
}

$Desktop = [Environment]::GetFolderPath('Desktop')
$Services |
    Export-Csv -Path (Join-Path $Desktop 'Services.csv') -NoTypeInformation
$Tasks |
    Export-Csv -Path (Join-Path $Desktop 'ScheduledTasks.csv') -NoTypeInformation

Write-Host "Done. Found $($Services.Count) services and $($Tasks.Count) scheduled tasks."
Code language: PowerShell (powershell)

Wrapping Up

Run Find-ServiceTaskLogon.ps1 from an elevated PowerShell prompt, and you will know every place that account is hiding before you change its password. Once you clean those up (or migrate them to a gMSA where supported), you can rotate the password without locking out the network.

Published on

by

Nick

Leave a Comment