Skip to content

Instantly share code, notes, and snippets.

@roe3p
Created June 25, 2019 11:12
Show Gist options
  • Save roe3p/09c546f553e66c71c1a02153339653a8 to your computer and use it in GitHub Desktop.
Save roe3p/09c546f553e66c71c1a02153339653a8 to your computer and use it in GitHub Desktop.
Export DB Users using DBATools
$ServerName = "Myserver.mydomain.co.uk"
$OutputPath = "G:\Backups\SRV01Users\"
$Dt = Get-Date -f yyyyMMdd #-hhmmss
$Databases = Get-DbaDatabase -sqlinstance localhost
$Counter = 1
$DBCount = $Databases.Count
foreach($Database in $Databases) {
$DatabaseName = $Database.Name
$Filename = $OutputPath + $dt+"_"+$DatabaseName+".sql"
Write-Host "Exporting users for DB: $DatabaseName [$Counter of $DBCount]" -ForegroundColor Green
Write-Host "To file: $Filename..." -ForegroundColor Green
$Database | Export-DbaUser -Filepath $Filename
$Counter++
# Export-DbaUser -SQLInstance $ServerName -Database $DatabaseName -Filepath $Filename
}
$Filename = $OutputPath + $dt +"_"+$ServerName+".sql"
Write-Host "Exporting users for all databases on: $ServerName" -ForegroundColor Green
Write-Host "To file: $Filename..." -ForegroundColor Green
Export-DbaUser -SQLInstance $ServerName -Filepath $Filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment