0

I can't seem to find any information anywhere on how to add header filters to a CSV file exported from powershell. See script below which exports a report on specific AD group members:

$Date = (Get-Date).ToString("dd/MM/yyyy hh:mm:ss")
$DateSh = (Get-Date).ToString("yyyyMMdd")

$CSVfile = “C:\Documents\Group Membership Report\Reports\$DateSh-GroupAccountsReport.csv”

$groups = ("Group1","Group2")
$groups | ForEach-Object { 
    $curgroup = $_; $_ 
    } | Get-ADGroupMember -Recursive | Get-ADUser -Properties * -ErrorAction SilentlyContinue | Select Name,ObjectClass,Enabled,SamAccountName,LastLogonDate, @{N='Group';E={$curgroup}} | Export-Csv -NoTypeInformation $CSVfile

The rest of the script sends an email with this CSV as an attachment.

I would like to be able to add filters to the headers in the CSV file to make it easier to sort, which I can do manually, but I need the script to do this itself, see image below:

CSV Headers on top of data

I'm not sure if how simple a task it is, if it's just a case of adding a few extra lines to my existing script or needing a whole new one. I think it would need to be converted to an xls doc and formatted as a table. Any help is appreciated,

Thanks

Mark Corrigan
  • 13
  • 1
  • 4
  • 7
    These filters are a function of Excel - not the CSV file by itself. A CSV file is plain text. You cannot add filters. You may take a look at the module [ImportExcel](https://www.powershellgallery.com/packages/ImportExcel/) – Olaf Aug 11 '21 at 16:15
  • Looks like it’s not as simple as a few lines of code, will need to convert it to an Excel doc to format as desired – Mark Corrigan Aug 11 '21 at 16:36
  • I don't think `ImportExcel` lets you change the filters, only toggle on/off for the table. This might require the `Excel.Application` COM object. – codewario Aug 11 '21 at 17:00
  • If its Excel Autofilters what you're looking for the ImportExcel can do that for you as Olaf suggested. – Santiago Squarzon Aug 11 '21 at 17:19
  • If the rest of the data is not needed you could also just filter the data before converting to CSV and just send that data. – Seth Aug 12 '21 at 09:07

0 Answers0