Powershell – How to export the results of the script to file

powershellxml

I need to get some details out of hundreds of XML files that were written for reporting. The script below provides the correct results, but I want them in file, cvs, xls, or txt so I can provide a list of the database fields that are in use by these reports.

$get = get-childitem D:\Data\Desktop\Projects\Reports\Test -include *.xml -recurse 
ForEach ($xmlfile in $get)
{
$Report = $xmlfile
$Fields = [xml](Get-Content $Report)
$Fields.reportsettings.fieldlist.field | select @{ L = 'Description'; E = {$_.desc}},
                                                @{ L = 'Field Name'; E = {$_.criterion}},
                                                @{ L = 'Field ID'; E = {$_.id} } } 

Thank you very much for your time!

Best Answer

Here's a little function called Out-ExcelReport. Just pipe anything to it, and it will open in Microsoft Excel - in case it is installed on your system:

function Out-ExcelReport
{
  param
  (
    $Path = "$env:temp\$(Get-Random).csv"
  )

  $Input | Export-Csv -Path $Path -Encoding UTF8 -NoTypeInformation -UseCulture
  Invoke-Item -Path $Path
}
Get-Process | Out-ExcelReport