#PSTip Add a SQL login to database roles using SMO

Note: This tip requires PowerShell 2.0 or above.

We can use SQL Server SMO object to add a SQL login to the database roles. For example, roles such as dbcreator, sysadmin, etc.

Let us see how we can do it using PowerShell and SMO.

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$smo = New-Object Microsoft.SqlServer.Management.Smo.Server $env:ComputerName

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts