<commit_before>﻿Function Get-DbaDatabaseUser {
	<#
.SYNOPSIS
Gets database users

.DESCRIPTION
Gets database users

.PARAMETER SqlInstance
The target SQL Server instance(s)

.PARAMETER SqlCredential
Allows you to login to SQL Server using alternative credentials

.PARAMETER Database
To get users from specific database(s)

.PARAMETER ExcludeDatabase
The database(s) to exclude - this list is auto populated from the server

.PARAMETER Silent
Use this switch to disable any kind of verbose messages

.NOTES
Tags: security, Databases
Author: <NAME> ( @PowerDbaKlaas )

Website: https://dbatools.io
Copyright: (C) <NAME>, <EMAIL>
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance sql2016

Gets all database users

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance Server1 -Database db1

Gets the users for the db1 database

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance Server1 -ExcludeDatabase db1

Gets the users for all databases except db1

.EXAMPLE
'Sql1','Sql2/sqlexpress' | Get-DbaDatabaseUser

Gets the users for the databases on Sql1 and Sql2/sqlexpress

#>
	[CmdletBinding()]
	param (
		[parameter(Mandatory, ValueFromPipeline)]
		[Alias("ServerInstance", "SqlServer")]
		[DbaInstanceParameter[]]$SqlInstance,
		[PSCredential][System.Management.Automation.CredentialAttribute()]$SqlCredential,
		[object[]]$Database,
		[object[]]$ExcludeDatabase,
		[switch]$Silent
	)

	process {
		foreach ($instance in $SqlInstance) {
			try {
				Write-Message -Level Verbose -Message "Connecting to $instance"
				$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
			}
			catch {
				Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
			}
			
			$databases = $server.Databases
			
			if ($Database) {
				$databases = $databases | Where-Object Name -In $Database
			}
			if ($ExcludeDatabase) {
				$databases = $databases | Where-Object Name -NotIn $ExcludeDatabase
			}

			foreach ($db in $databases) {
				if (!$db.IsAccessible) {
					Write-Message -Level Warning -Message "Database $db is not accessible. Skipping."
					continue
				}

				$users = $db.users

				if (!$users) {
					Write-Message -Message "No users exist in the $db database on $instance" -Target $db -Level Verbose
					continue
				}
                $users | foreach {

				Add-Member -InputObject $_ -MemberType NoteProperty -Name ComputerName -value $server.NetName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name Database -value $db.Name

				Select-DefaultView -InputObject $_ -Property ComputerName, InstanceName, SqlInstance, Database, CreateDate, DateLastModified, Name, Login, LoginType, AuthenticationType, State, HasDbAccess, DefaultSchema
                }
			}
		}
	}
}<commit_msg>add switch exludesystemuser and example<commit_after>﻿Function Get-DbaDatabaseUser {
	<#
.SYNOPSIS
Gets database users

.DESCRIPTION
Gets database users

.PARAMETER SqlInstance
The target SQL Server instance(s)

.PARAMETER SqlCredential
Allows you to login to SQL Server using alternative credentials

.PARAMETER Database
To get users from specific database(s)

.PARAMETER ExcludeDatabase
The database(s) to exclude - this list is auto populated from the server

.PARAMETER ExcludeSystemUser
This switch removes all system objects from the user collection

.PARAMETER Silent
Use this switch to disable any kind of verbose messages

.NOTES
Tags: security, Databases
Author: <NAME> ( @PowerDbaKlaas )

Website: https://dbatools.io
Copyright: (C) <NAME>, <EMAIL>
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance sql2016

Gets all database users

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance Server1 -Database db1

Gets the users for the db1 database

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance Server1 -ExcludeDatabase db1

Gets the users for all databases except db1

.EXAMPLE
Get-DbaDatabaseUser -SqlInstance Server1 -ExcludeSystemUser

Gets the users for all databases that are not system objects, like 'dbo', 'guest' or 'INFORMATION_SCHEMA'

.EXAMPLE
'Sql1','Sql2/sqlexpress' | Get-DbaDatabaseUser

Gets the users for the databases on Sql1 and Sql2/sqlexpress

#>
	[CmdletBinding()]
	param (
		[parameter(Mandatory, ValueFromPipeline)]
		[Alias("ServerInstance", "SqlServer")]
		[DbaInstanceParameter[]]$SqlInstance,
		[PSCredential][System.Management.Automation.CredentialAttribute()]$SqlCredential,
		[object[]]$Database,
		[object[]]$ExcludeDatabase,
        [switch]$ExcludeSystemUser,
		[switch]$Silent
	)

	process {
		foreach ($instance in $SqlInstance) {
			try {
				Write-Message -Level Verbose -Message "Connecting to $instance"
				$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
			}
			catch {
				Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
			}
			
			$databases = $server.Databases
			
			if ($Database) {
				$databases = $databases | Where-Object Name -In $Database
			}
			if ($ExcludeDatabase) {
				$databases = $databases | Where-Object Name -NotIn $ExcludeDatabase
			}

			foreach ($db in $databases) {
				if (!$db.IsAccessible) {
					Write-Message -Level Warning -Message "Database $db is not accessible. Skipping."
					continue
				}

				$users = $db.users

				if (!$users) {
					Write-Message -Message "No users exist in the $db database on $instance" -Target $db -Level Verbose
					continue
				}
                if ( $PSBoundParameters.ContainsKey('ExcludeSystemUser') ) {
                    $users = $users.where({$_.IsSystemObject -eq $false})
                }

                $users | foreach {

				Add-Member -InputObject $_ -MemberType NoteProperty -Name ComputerName -value $server.NetName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
				Add-Member -InputObject $_ -MemberType NoteProperty -Name Database -value $db.Name

				Select-DefaultView -InputObject $_ -Property ComputerName, InstanceName, SqlInstance, Database, CreateDate, DateLastModified, Name, Login, LoginType, AuthenticationType, State, HasDbAccess, DefaultSchema
                }
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.09288446824789595, 'fraction_numerical': 0.005355776587605203, 'mean_word_length': 3.8667163067758747, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4917744', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1919, 'n_words': 592}