How to add a ROW in a exported csv from a Powershell query

Normally I would just trial and error this till I got it, but not finding exactly what I’m looking for searching here.

Need help formatting a CSV(or Saved Array Variable) to “group” similar results and leave a space between, to allow better visibility in the output without having to manually modify the csv after wards.

Cant get this to come out without the extra space between lines, so its hard to demonstrate.

Creating a report of hotfixes for certain servers, that list the hotfixes, with dates. But need to break up the report so there is a ROW/line space between servers with different names.

Seems like something that should be fairly easy to do. attempted to just add a line space via `n after the query statement before the end of the foreach loop but that did not work. Error’d out

$results = foreach ($server in $servers.name){

$limit = (Get-Date).AddDays(-60)
get-hotfix -computername $server -Erroraction Continue |select PScomputername, installedon, hotfixID, Description,Caption |where {$_.InstalledOn -gt $limit} |sort PSComputername, installedon

}

$date=(get-date -Format d) -replace “/” , “-”
$time = (get-date -format t) -replace “:” , “-”
$results |export-csv “C:\Monthly Patch Reports\Powershell_Test$date $time PreTESTPatchReport.csv” -NoTypeInformation

Example Intended Results

computer1 - data - data - data
computer1 - data - data - data
computer1 - data - data - data

computer2 - data - data - data

computer3 - data - data - data
computer3 - data - data - data

Example ACTUAL Results

computer1 - data - data - data
computer1 - data - data - data
computer1 - data - data - data
computer2 - data - data - data
computer3 - data - data - data
computer3 - data - data - data

If you want this to remain a true CSV, you will have to fill those columns with empty data on the extra lines. If $servers.Name contains any duplicates, you can do the following. The $previousServer technique only matters if you could have duplicate names in your list. If all names are unique, then you only need to add the empty data line after your Get-HotFix command.

$previousServer = $null

$results = foreach ($server in $servers.name) {
    if ($previousServer -and $previousServer -ne $server) {
      "" | Select-Object PScomputername,installedon,hotfixID,Description,Caption
    }
    $limit = (Get-Date).AddDays(-60)
    get-hotfix -computername $server -Erroraction Continue | select PScomputername, installedon, hotfixID, Description,Caption |
        where InstalledOn -gt $limit | Sort PSComputername, installedon
    $previousServer = $server
}

 

If $servers.name contains unique values only, you can just do the following. This will add a final line of empty data in case you need to append more data later.

$results = foreach ($server in $servers.name) {
    $limit = (Get-Date).AddDays(-60)
    get-hotfix -computername $server -Erroraction Continue | select PScomputername, installedon, hotfixID, Description,Caption |
        where InstalledOn -gt $limit | Sort PSComputername, installedon
    "" | Select-Object PScomputername,installedon,hotfixID,Description,Caption
}