---
title: "Disconnect-DbaInstance"
description: "Closes active SQL Server connections and removes them from the dbatools connection cache"
url: "https://dbatools.io/Disconnect-DbaInstance/"
availability: "Windows, Linux, macOS"
tags: ["Connection"]
author: "Chrissy LeMaire (@cl), netnerds.net"
source: "https://github.com/dataplat/dbatools/blob/master/public/Disconnect-DbaInstance.ps1"
last_updated: "2024-01-01"
---

# Disconnect-DbaInstance

## Synopsis

Closes active SQL Server connections and removes them from the dbatools connection cache

## Description

Properly closes SQL Server connections created by dbatools commands like Connect-DbaInstance, preventing connection leaks and freeing up server connection limits. This function handles both SMO server objects and raw SqlConnection objects, ensuring clean disconnection and removing connections from the internal connection hash. Use this in scripts to explicitly manage connection lifecycle, especially when working with multiple instances or in long-running automation where connection limits matter.  
  
To clear all of your connection pools, use Clear-DbaConnectionPool

## Syntax

```powershell
Disconnect-DbaInstance
    [[-InputObject] <PSObject[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

```

## Examples

### Example 1: Disconnects all connected instances

```powershell
PS C:\> Get-DbaConnectedInstance | Disconnect-DbaInstance
```

### Example 2: Disconnects selected SQL Server instances

```powershell
PS C:\> Get-DbaConnectedInstance | Out-GridView -Passthru | Disconnect-DbaInstance
```

### Example 3: Disconnects the $server connection

```powershell
PS C:\> $server = Connect-DbaInstance -SqlInstance sql01
PS C:\> $server | Disconnect-DbaInstance
```

### Optional Parameters

##### -InputObject

Specifies the SQL Server connection object(s) to disconnect, such as SMO Server objects or SqlConnection objects from Connect-DbaInstance. Accepts pipeline input from Get-DbaConnectedInstance to   
disconnect multiple connections at once.  
Use this to explicitly close specific connections rather than letting them time out, which helps prevent connection pool exhaustion and reduces load on SQL Server instances.

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

##### -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 were to run. No actions are actually performed.

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

##### -Confirm

Prompts you for confirmation before executing any changing operations within the command.

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

## Outputs

**PSCustomObject**

**Returns one object per connection successfully disconnected. Contains the following properties:**

**Default display properties (via Select-DefaultView):**

- SqlInstance: The name of the SQL Server instance that was disconnected
- ConnectionType: The full type name of the connection object (e.g., Microsoft.SqlServer.Management.Smo.Server or System.Data.SqlClient.SqlConnection)
- State: The state of the connection after disconnection (Disconnected or Closed)

**Additional properties available:**

- ConnectionString: The masked/hidden connection string used for the connection

---

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