title: Powershell: Out-File with Headers/Columns
body: <p>I am very close to what I am looking for, I have all of the correct data and it is just not formatting correctly.  Currently it is all in one column, and what I would like is for the headers to be in a row, and then each entry to be in its own row as well.  Here is my current code:</p>
  
  <pre><code>$servers = Get-ADComputer -Filter "Name -like 'f****p*'" | Sort-Object | Select -ExpandProperty Name
  
  $servers | foreach {
  
  $server = $_
  $serverIP = Test-Connection -ComputerName $server -Count 1 | Select -ExpandProperty IPV4Address
  $serverIPString = $serverIP.IPAddresstoString
  $scope = Get-DhcpServerv4Scope -ComputerName $server
  $options = Get-DhcpServerv4OptionValue -ComputerName $server
  
  New-Object -TypeName psobject -Property @{
  
      ServerName = $server
      ServerIP = $serverIPString
      ScopeName = $scope.Name | Out-String
      StartIP = $scope.StartRange.IPAddressToString | Out-String
      EndIP = $scope.EndRange.IPAddressToString | Out-String
      SubnetMask = $scope.SubnetMask.IPAddressToString | Out-String
      Duration = $scope.LeaseDuration.Days | Out-String
      OptionName = $options.Name | Out-String
      OptionID = $options.OptionID | Out-String
      OptionValue = $options.Value | Out-String
  
      }
  
  } | Select-Object ServerName, ServerIP, ScopeName, StartIP, EndIP, SubnetMask, Duration, OptionName, OptionID, OptionValue | Out-File C:\temp\FPScopes.csv
  </code></pre>
  
  <p>Currently the output looks like this example (using Duration and Options as an example, as it has the most generic data to share):</p>
  
  <p><a href="https://i.stack.imgur.com/FQjUM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FQjUM.png" alt="enter image description here"></a></p>
  
  <p>What I am looking for would be to have each object type as a column header, and have the entries in a separate row underneath the header:</p>
  
  <p><a href="https://i.stack.imgur.com/70Fte.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/70Fte.png" alt="enter image description here"></a></p>
  
  <p>Etc....</p>
  
  <p>Any assistance would be greatly appreciated.  </p>
  
  <p>Thank you!  :)
  Ry</p>
  
  <p>My final code, after help from this forum:</p>
  
  <pre><code>Add-Content -Value "ServerName,ServerIP,ScopeName,StartRange,EndRange,SubnetMask,Duration,OptionName,OptionID,Value" -Path c:\temp\POSScopes.csv
  
  Get-ADComputer -Filter {Name -like 'p****p01' -or Name -like 'p****p02' -or Name -like 'q****001' -or Name -like 'q****002'} | Sort-Object | Select -ExpandProperty Name | Out-File C:\Temp\POSServers.txt
  
  $servers = Get-Content C:\Temp\POSServers.txt
  
  $servers | foreach {
  
  $server = $_
  $serverIP = Test-Connection -ComputerName $server -Count 1 | Select -ExpandProperty IPV4Address
  $serverIPString = $serverIP.IPAddresstoString
  $scope = Get-DhcpServerv4Scope -ComputerName $server
  $options = Get-DhcpServerv4OptionValue -ComputerName $server
  
      New-Object -TypeName psobject -Property @{
  
          ServerName = $server
          ServerIP = $serverIPString
          ScopeName = $scope.Name | Out-String
          StartIP = $scope.StartRange.IPAddressToString | Out-String
          EndIP = $scope.EndRange.IPAddressToString | Out-String
          SubnetMask = $scope.SubnetMask.IPAddressToString | Out-String
          Duration = $scope.LeaseDuration.Days | Out-String
          OptionName = $options.Name | Out-String
          OptionID = $options.OptionID | Out-String
          OptionValue = $options.Value | Out-String
  
  }
  
  
  for ($i = ($Options.Count -1); $i -gt -1; $i--) {
  
  Add-Content -Value "$($server),$($serverIPString),$($scope.Name),$($scope.StartRange.IPAddressToString),$($scope.EndRange.IPAddressToString),$($scope.SubnetMask.IPAddressToString),$($scope.LeaseDuration.Days),$($options[$i].Name),$($options[$i].OptionID),$($Options[$i].Value)" -Path C:\temp\POSScopes.csv
  
      } 
  
  }
  </code></pre>