Export to Excel script snippet
Mar 24, 2021
Using a VSCode snippet to improve writing Export-Excel functions
If you use the ImportExcel PowerShell module (and you really should be if you are handling data in your scripts and sharing it out with colleagues via files) then you will probably be writing code that takes an object variable which is the results of your script and passing it to the Export-Excel function.
Some key parameters for the function are the path for the output file and a table style for that data. I found myself using this so much as a splatting variable that I created this VSCode snippet to ease the process.
Open VSCode, press F1 and start typing ‘User snipp’.
Click the User Snippets menu item and then choose PowerShell.json from that menu.
Add the snippet code section below into that JSON file
"ExportExcel":{
"prefix": "_ExportExcelSplat",
"body": [
"\\$splatExcel = @{",
" path = \"${Outfile}\"",
" tablestyle = \"medium15\"",
" autosize = \\$true",
"}",
"\\$result | export-excel @splatExcel -worksheetname \"${1:Worksheetname}\" -tablename \"${2:Tablename}\"",
"$0"],
"description": "A basic splat table and Export-Excel command"
}
Save the file and then open a new file in the code editor, then type _exp
and you will see this tool tip
Press [Tab] and the snippet of code will be pasted in and your cursor position will be ready to complete the Worksheetname value. Once you have done that press [Tab] again and you can complete the TableName value. Once you have completed that press [Tab] again and you can provide the path value for the file you want created. One final press of the [Tab] button will take you to the line after the pasted snippet for you to continue your script.
Hope this helps!
Share