---
title: "Set-DbaSpn"
description: "Sets an SPN for a given service account in active directory (and also enables delegation to the same SPN by default)"
url: "https://dbatools.io/Set-DbaSpn/"
availability: "Windows only"
tags: ["SPN"]
author: "Drew Furgiuele (@pittfurg), port1433.com"
source: "https://github.com/dataplat/dbatools/blob/master/public/Set-DbaSpn.ps1"
last_updated: "2024-01-01"
---

# Set-DbaSpn

## Synopsis

Sets an SPN for a given service account in active directory (and also enables delegation to the same SPN by default)

## Description

This function will connect to Active Directory and search for an account. If the account is found, it will attempt to add an SPN. Once the SPN is added, the function will also set delegation to that service, unless -NoDelegation is specified. In order to run this function, the credential you provide must have write access to Active Directory.  
  
Note: This function supports -WhatIf

## Syntax

```powershell
Set-DbaSpn
    [-SPN] <String>
    [-ServiceAccount] <String>
    [[-Credential] <PSCredential>]
    [-NoDelegation]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

```

## Examples

### Example 1: Connects to Active Directory and adds a provided SPN to the given account

```powershell
PS C:\> Set-DbaSpn -SPN MSSQLSvc/SQLSERVERA.domain.something -ServiceAccount domain\account
PS C:\> Set-DbaSpn -SPN MSSQLSvc/SQLSERVERA.domain.something -ServiceAccount domain\account -EnableException
```

Connects to Active Directory and adds a provided SPN to the given account.  
Connects to Active Directory and adds a provided SPN to the given account, suppressing all error messages and throw exceptions that can be caught instead  

### Example 2: Connects to Active Directory and adds a provided SPN to the given account

```powershell
PS C:\> Set-DbaSpn -SPN MSSQLSvc/SQLSERVERA.domain.something -ServiceAccount domain\account -Credential ad\sqldba
```

Connects to Active Directory and adds a provided SPN to the given account. Uses alternative account to connect to AD.  

### Example 3: Connects to Active Directory and adds a provided SPN to the given account, without the delegation

```powershell
PS C:\> Set-DbaSpn -SPN MSSQLSvc/SQLSERVERA.domain.something -ServiceAccount domain\account -NoDelegation
```

### Example 4: Sets all missing SPNs for sql2016

```powershell
PS C:\> Test-DbaSpn -ComputerName sql2016 | Where-Object { $_.isSet -eq $false } | Set-DbaSpn
```

### Example 5: Displays what would happen trying to set all missing SPNs for sql2016

```powershell
PS C:\> Test-DbaSpn -ComputerName sql2016 | Where-Object { $_.isSet -eq $false } | Set-DbaSpn -WhatIf
```

### Required Parameters

##### -SPN

Specifies the Service Principal Name to register in Active Directory for SQL Server Kerberos authentication.  
Must follow the format 'MSSQLSvc/hostname:port' or 'MSSQLSvc/FQDN:port' for named instances.  
Use this to enable Kerberos authentication and eliminate double-hop authentication issues.

| Property | Value |
| --- | --- |
| Alias | RequiredSPN |
| Required | True |
| Pipeline | true (ByPropertyName) |
| Default Value |  |

##### -ServiceAccount

Specifies the Active Directory account that runs the SQL Server service and will own the SPN.  
Can be a domain user account (domain\username) or computer account (computername$) depending on your SQL Server service configuration.  
This account must exist in Active Directory and you must have permissions to modify its properties.

| Property | Value |
| --- | --- |
| Alias | InstanceServiceAccount,AccountName |
| Required | True |
| Pipeline | true (ByPropertyName) |
| Default Value |  |

### Optional Parameters

##### -Credential

The credential you want to use to connect to Active Directory to make the changes

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | true (ByPropertyName) |
| Default Value |  |

##### -NoDelegation

Prevents automatic configuration of Kerberos constrained delegation for the specified SPN.  
Use this when you want to manually configure delegation later or when delegation is not required for your environment.  
By default, the function enables constrained delegation to allow the service account to authenticate to other services.

| 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 |

##### -WhatIf

Shows what would happen if the command was executed

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

##### -Confirm

Turns confirmations before changes on or off

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

## Outputs

**PSCustomObject**

**Returns one or two objects per service account processed, depending on delegation configuration:**

**SPN Registration Result Object (always returned):**

- Name (string) - The Service Principal Name that was added
- ServiceAccount (string) - The Active Directory account that owns the SPN
- Property (string) - Always "servicePrincipalName" indicating the AD property that was modified
- IsSet (boolean) - True if the SPN was successfully added, False if the operation failed
- Notes (string) - Status message: "Successfully added SPN" or "Failed to add SPN"

**Delegation Configuration Result Object (conditionally returned):**

- Name (string) - The Service Principal Name for which delegation was configured
- ServiceAccount (string) - The Active Directory account for which delegation was configured
- Property (string) - Always "msDS-AllowedToDelegateTo" indicating the AD property that was modified
- IsSet (boolean) - True if constrained delegation was successfully enabled, False if the operation failed
- Notes (string) - Status message: "Successfully added constrained delegation" or "Failed to add constrained delegation"

**The delegation object is only returned when:**

- The SPN was successfully added (IsSet = $true for the first object)
- AND the -NoDelegation parameter was NOT specified
- AND the ShouldProcess check passed (when -WhatIf is not used)
When -NoDelegation is specified, only the SPN registration object is returned.

---

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