Commands / Get-sqmLoginPermissions
Get-sqmLoginPermissions
Security & Audit sqmSQLTool v1.9.x · Get
Login-centric permission report. For each login: its server roles (sys.server_role_members) and explicit server permissions, plus, per database, the mapped database user, its database roles and its explicit GRANT/DENY permissions. One flat row per permission, ready for CSV/HTML export and filtering.
Mapped by SID, not by name. A login and its database user can have different names (or the same name but different identities). Every join in this function goes through the SID, so the report reflects what SQL Server itself considers the same principal, not what the names suggest.
Orphaned users (a database user with no matching server login) are excluded by default. Add -IncludeOrphanedUsers to include them, they get LoginName = '' and IsOrphaned = $true. With an active -Login filter, orphans are never shown, they don't belong to any of the requested logins by definition.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestring[]Optional$env:COMPUTERNAMEOne or more instances. Pipeline-capable.
-SqlCredentialPSCredentialOptional, SQL login credentials.
-Loginstring[]Optional@() (all)Filter: only these logins. Wildcards allowed.
-Databasestring[]Optional@() (all)Filter: only these databases. Wildcards allowed.
-ExcludeDatabasestring[]Optional@()Databases to skip. Wildcards allowed.
-ExcludeSystemDatabasesswitchSwitch$falseSkip master, model, msdb, tempdb.
-ExcludeSystemLoginsswitchSwitch$falseHide NT SERVICE\*, NT AUTHORITY\* (incl. localized variants) and ##MS_*##.
-IncludeOrphanedUsersswitchSwitch$falseAlso report database users with no matching server login.
-ExcludeServerScopeswitchSwitch$falseSkip server-level roles/permissions, database scope only.
-OutputPathstringOptional, If given, CSV and HTML reports are written and copied to the central path.
-ContinueOnErrorswitchSwitch$falseContinue with the next instance on error instead of aborting the whole run.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately.
-NoOpenswitchSwitch$falseDo not automatically open the generated report after creation.

Execution Flow

START dbatools available? NO throw: Install dbatools YES foreach $instance in -SqlInstance (pipeline-capable) Read all logins from sys.server_principals, index by SID Apply -ExcludeSystemLogins and -Login filters No logins left? → warn, continue to next instance -ExcludeServerScope set? YES Skip server scope entirely NO Server roles + explicit server permissions sys.server_role_members UNION sys.server_permissions Keep only rows whose SID matches a filtered-in login → one row per permission Get-DbaDatabase → accessible databases on the instance foreach $db in the (filtered) database list -Database / -ExcludeDatabase / -ExcludeSystemDatabases filters Query fails for this DB (offline, no access)? → warn, skip it, keep going Database roles + explicit database permissions per user sys.database_role_members UNION sys.database_permissions Each row's SID looked up against the login map from step 1 SID matches a login? YES NO -IncludeOrphaned Users AND no -Login? YES NO → skip row Add [PSCustomObject] row: Scope=Database IsOrphaned = true when no login matched → next user row, then next database, then next instance -OutputPath given? NO YES Export-Csv + ConvertTo-sqmHtmlReport Copy-sqmToCentralPath Open HTML unless -NoOpen Return $allResults DONE

Output object

FieldContents
SqlInstance / LoginName / LoginType / IsDisabledWhich login, on which instance.
ScopeServer or Database.
DatabaseName / UserNameSet for database-scope rows only.
IsOrphanedDatabase user with no matching server login (only present when -IncludeOrphanedUsers was given).
PermissionTypeRole (membership) or Permission (explicit GRANT/DENY).
Permission / State / ClassThe role or permission name, its state (GRANT/DENY/MEMBER), and the securable class.
SecurableSchema / SecurableSchema and name of the object the permission applies to, where applicable.

Examples

Full permission report for one instance
Get-sqmLoginPermissions -SqlInstance "SQL01"
Application logins only, skip system databases
Get-sqmLoginPermissions -SqlInstance "SQL01" -Login "APP_*" -ExcludeSystemDatabases
Include orphaned database users, export evidence
Get-sqmLoginPermissions -SqlInstance "SQL01" -IncludeOrphanedUsers -OutputPath "C:\Reports"
Just the orphans, across two instances
"SQL01", "SQL02" | Get-sqmLoginPermissions -IncludeOrphanedUsers | Where-Object IsOrphaned

Requirements

Requires dbatools. Needs VIEW ANY DEFINITION plus CONNECT on each database to be reported. Databases that are offline or inaccessible are skipped with a warning, not a hard failure.

See also

Get-sqmLoginLastAccess, when a login was last provably used.
Get-sqmSysadminAccounts, who currently holds sysadmin.