Managing logins, users, and permissions

PowerShell and SMO can help pull a list of SQL Server logins, database users, and permissions. Since a login is an instance-level object, you can use the SMO Server object to pull information about every login registered in your instance. You can also list all the server roles this login belongs to, as shown in the following snippet:

Import-Module SQLPS -DisableNameChecking

#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft.SqlServer.Management.Smo.Server" $servername

$result = @()

$server.Logins |
Where-Object IsSystemObject -EQ $false |
ForEach-Object {
   $login = $_
   $object = [pscustomobject] @{
      Login = $login.Name
      LoginType = $login.LoginType
 CreateDate = $login.CreateDate ...

Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.