I have an excel with multiples columns and I would like to export some specific columns to a .xlsx file but it export the first 3 columns of the excel and not the columns with the specific headers: 'Host','CPU usage %',"Memory usage %"
$SourceFileDirectory = "C:\TEMP\"
$CurrentDate = Get-Date -Format "yyyyMMdd"
$TestFile = "Test2"
$ExcelExt = ".xlsx"
$ExcelFiles = Get-ChildItem $SourceFileDirectory -Filter vHost.xlsx
$headers = 'Host','CPU usage %',"Memory usage %"
foreach ($file in $ExcelFiles)
{
$ImportFile = -JOIN($SourceFileDirectory,$file)
$DestinationFile = -JOIN($SourceFileDirectory,$TestFile,"_",$CurrentDate,$ExcelExt)
$sheetName = 'vHost.xlsx' # => Define the WorkSheet Name here
Write-Host $ImportFile
Write-Host $DestinationFile
$xlsx = Import-Excel -Path $ImportFile -HeaderName $headers -StartRow 1 |
Select-Object * -ExcludeProperty Dupe* |
Export-Excel -Path $DestinationFile -PassThru -WorksheetName $sheetName
$ws = $xlsx.Workbook.Worksheets[$sheetName]
Set-ExcelRange -HorizontalAlignment Center -Worksheet $ws -Range $ws.Dimension.Address
Close-ExcelPackage $xlsx
}