Thor Logo dbatools

Remove-DbaLogin

View Source
Chrissy LeMaire (@cl), netnerds.net
Windows, Linux, macOS

Synopsis

Removes SQL Server logins from target instances

Description

Removes one or more SQL Server logins from specified instances using the SMO Drop() method. This function handles the complete removal process including dependency checks and provides proper error handling when logins cannot be dropped due to existing sessions or database ownership. Use the -Force parameter to automatically terminate active sessions associated with the login before removal, which is useful when cleaning up test environments or decommissioning user accounts.

Syntax

Remove-DbaLogin
    [-SqlCredential <PSCredential>]
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Remove-DbaLogin -SqlInstance <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    -Login <String[]>
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

Remove-DbaLogin
    [-SqlCredential <PSCredential>]
    -InputObject <Login[]>
    [-Force]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Remove-DbaLogin -SqlInstance sql2016 -Login mylogin

Prompts then removes the Login mylogin on SQL Server sql2016

Example: 2
PS C:\> Remove-DbaLogin -SqlInstance sql2016 -Login mylogin, yourlogin

Prompts then removes the Logins mylogin and yourlogin on SQL Server sql2016

Example: 3
PS C:\> Remove-DbaLogin -SqlInstance sql2016 -Login mylogin -Confirm:$false

Does not prompt and swiftly removes mylogin on SQL Server sql2016

Example: 4
PS C:\> Get-DbaLogin -SqlInstance server\instance -Login yourlogin | Remove-DbaLogin

Removes mylogin on SQL Server server\instance

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value
-Login

Specifies the SQL Server login names to remove from the target instance. Accepts multiple login names as an array.
Use this when you know the specific logins to delete, such as when cleaning up test accounts or decommissioned user logins.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
Default Value
-InputObject

Accepts login objects piped from Get-DbaLogin or other dbatools functions that return SQL Server login objects.
Use this for advanced filtering scenarios or when chaining multiple dbatools commands together in a pipeline.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByValue)
Default Value

Optional Parameters

-SqlCredential

Allows you to login to servers using alternative credentials.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Force

Automatically terminates any active database connections and sessions associated with the login before attempting removal.
Use this when you need to forcibly remove logins that have active sessions, common in development environments or during emergency cleanup.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

PropertyValue
Aliaswi
RequiredFalse
Pipelinefalse
Default Value
-Confirm

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

PropertyValue
Aliascf
RequiredFalse
Pipelinefalse
Default Value