---
title: "Compare-DbaLogin"
description: "Compares SQL Server logins between a source and one or more destination instances."
url: "https://dbatools.io/Compare-DbaLogin/"
availability: "Windows, Linux, macOS"
tags: ["Login", "Security", "Compare"]
author: "the dbatools team + Claude"
source: "https://github.com/dataplat/dbatools/blob/master/public/Compare-DbaLogin.ps1"
last_updated: "2024-01-01"
---

# Compare-DbaLogin

## Synopsis

Compares SQL Server logins between a source and one or more destination instances.

## Description

Compares SQL Server logins between a source instance and one or more destination instances to identify which logins exist only on the source, only on the destination, or on both. This is useful for identifying logins that would be lost when using Copy-DbaLogin with -Force, or for auditing login consistency between environments.  
  
Returns one object per login per destination instance, indicating whether the login exists on the source, destination, or both.

## Syntax

```powershell
Compare-DbaLogin
    [-Source] <DbaInstanceParameter>
    [[-SourceSqlCredential] <PSCredential>]
    [-Destination] <DbaInstanceParameter[]>
    [[-DestinationSqlCredential] <PSCredential>]
    [[-Login] <String[]>]
    [[-ExcludeLogin] <String[]>]
    [-ExcludeSystemLogin]
    [-EnableException]
    [<CommonParameters>]

```

## Examples

### Example 1: Compares all logins between sql1 and sql2, returning the status of each login

```powershell
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2
```

### Example 2: Returns logins that exist on sql2 but not on sql1

```powershell
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2 | Where-Object Status -eq "DestinationOnly"
```

Returns logins that exist on sql2 but not on sql1. These logins would be lost if Copy-DbaLogin -Force were run from sql1 to sql2.  

### Example 3: Returns logins that exist on sql1 but not on sql2

```powershell
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2 | Where-Object Status -eq "SourceOnly"
```

Returns logins that exist on sql1 but not on sql2. These are the logins that Copy-DbaLogin would create.  

### Example 4: Compares user-created logins between sql1 and sql2, excluding built-in system logins

```powershell
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2 -ExcludeSystemLogin
```

### Example 5: Compares the specified logins between sql1 and both sql2 and sql3

```powershell
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2, sql3 -Login "appuser", "reportuser"
```

### Required Parameters

##### -Source

The source SQL Server instance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | false |
| Default Value |  |

##### -Destination

The destination SQL Server instance or instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | false |
| Default Value |  |

### Optional Parameters

##### -SourceSqlCredential

Login to the source instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -DestinationSqlCredential

Login to the destination instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Login

Specifies one or more logins to include in the comparison. All other logins are excluded.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeLogin

Specifies one or more logins to exclude from the comparison.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeSystemLogin

Excludes built-in system logins from the comparison results.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.  
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.  
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

## Outputs

**PSCustomObject**

Returns one object for each login found on either the source or destination instance.

**Properties:**

- SourceServer: The name of the source SQL Server instance
- DestinationServer: The name of the destination SQL Server instance
- LoginName: The name of the login account
- LoginType: The login type (SqlLogin, WindowsUser, WindowsGroup, etc.)
- Status: Indicates where the login exists - "SourceOnly", "DestinationOnly", or "Both"

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
