Skip to content

Instantly share code, notes, and snippets.

@imfioki
Created June 14, 2019 02:53
Show Gist options
  • Save imfioki/4cf9819b9803d0051b22fc67acf62975 to your computer and use it in GitHub Desktop.
Save imfioki/4cf9819b9803d0051b22fc67acf62975 to your computer and use it in GitHub Desktop.
Reporting in Powershell
# Here's a brisk tutorial on a basic method of reporting within Powershell.
# Yes, I know custom objects exist. :)
# Define the name for our table
$tableName = "<table_name>"
# Create the table object
$table = New-Object System.Data.DataTable "$tableName"
# Define columns for the table
$col1 = New-Object System.Data.DataColumn Column1,([string])
$col2 = New-Object System.Data.DataColumn Column2,([string])
$col3 = New-Object System.Data.DataColumn Column3,([string])
# Add the columns to the data table object
$table.Columns.Add($col1)
$table.Columns.Add($col2)
$table.Columns.Add($col3)
# Create a row to add to the table
$row = $table.NewRow()
# Add properties to the row object
$row.Column1 = "Value1"
$row.Column2 = "Value2"
$row.Column3 = "Value3"
# Add row to the table object
$table.Rows.Add($row)
# Export to CSV file for portability
$table | Export-Csv C:\Export.csv
# Export to JSON for portability
$table | ConvertTo-Json | Out-File C:\Export.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment