2025-05-07

Comparing Agent Jobs among SQL Server instances utilizing PowerShell

Utilizing dbatools along with the Compare-Object PowerShell cmdlet allows for the comparison of SQL Server Agent jobs across two SQL Server instances.

Set-ExecutionPolicy Unrestricted

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

Import-Module dbatools

Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true -Register

Set-DbatoolsConfig -FullName sql.connection.encrypt -Value $false -Register

$RefServer = Get-DbaAgentJob -SqlInstance REFERENCE_SQL -SqlCredential sa

$DiffServer = Get-DbaAgentJob -SqlInstance DIFFERENCE_SQL -SqlCredential sa

Compare-Object $RefServer $DiffServer

The result of the comparison indicates whether a property value appeared only in the reference object (<=) or only in the difference object (=>).