Export-CSV forEach psitem?

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 5 days, 18 hours ago.

  • Author
  • #70703
    Profile photo of Jeff Taylor
    Jeff Taylor

    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) }}
  • #70739
    Profile photo of Don Jones
    Don Jones

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

  • #70955
    Profile photo of random commandline
    random commandline
    $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

You must be logged in to reply to this topic.