Skip to content

Instantly share code, notes, and snippets.

@martin-guth
Last active December 30, 2023 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-guth/1857236b79ee3e51a6a7470459bd1550 to your computer and use it in GitHub Desktop.
Save martin-guth/1857236b79ee3e51a6a7470459bd1550 to your computer and use it in GitHub Desktop.
Basic test script on how to export to Excel using the module ImportExcel from a SQL query fired off by dbatools
Import-Module dbatools;
Import-Module ImportExcel;
$credential = Get-Credential;
# get results from table
$result= Invoke-DbaQuery -SQLInstance localhost -Database master -Query "SELECT * FROM sys.tables" -SqlCredential $credential ;
# debug the output
$result | Out-GridView;
$begin = Get-Date;
# export to excel
#$result | Export-OOXML -FileFullPath test.xlsx -WorksheetName test -AutoFit -HeaderStyle WhiteSmoke;
$result | Export-Excel $outputpath -WorksheetName test -AutoFilter -AutoSize -BoldTopRow -FreezeTopRowFirstColumn;
$end = Get-Date;
echo "duration $($end-$begin)";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment