Export-CSV forEach psitem?

How would I export this one-liner?

foreach ($domain in (Get-ADForest).domains) { 
  Get-ADDomainController -filter * -server $domain | 
  sort hostname  |
  foreach { 
    Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $psitem.Hostname |
    select @{name="DomainController";Expression={$_.PSComputerName}}, Manufacturer, Model,@{Name="TotalPhysicalMemory(GB)";Expression={ "{0:N0}" -f  ($_.TotalPhysicalMemory / 1Gb) }}

You want the morally correct way or the quick and dirty way?

$domain = (Get-ADForest).domains
$host = foreach ($d in $domain){(Get-ADDomainController -Server $d).hostname}

Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $host |
    Select-Object @{name="DomainController";Expression={$_.PSComputerName}},
    Expression={"{0:N0}" -f  ($_.TotalPhysicalMemory / 1Gb)}} |
    Export-Csv \\path\to\export.csv -NoTypeInformation

lol morally correct Don

Thanks random


Apparently, this script only outputs (1) DC per domain. I’d like output for all DC’s in each domain.

$hosts = foreach ($d in $domain){(Get-ADDomainController -Server $d -Filter *).hostname}