#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
Filed in: Columns, Tips and Tricks Tags: , , ,

3 Responses to "#PSTip Add a SQL login to database roles using SMO"

  1. Hey Ravikanth, thanks for sharing these two helpful lines of code! However there is an type error: “AddToRoles” should be “AddToRole” without a “s” at the end. BR Patrick

Leave a Reply

Submit Comment

© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: