title: Issues getting column width set when creating a csv file from Powershell
body: <p>I am having to update 500+ machines at work not knowing the patching status of them and what they will need patched. This will just be a script that will tell me which machines are online or offline so I know what to target with other scripts that are yet to be written.</p>
  
  <p>This will output to a CSV file and it can be opened successfully with all of the data in the proper columns.</p>
  
  <p>How do I get the column width of the columns in the CSV file to be a set size or autosized to the data in the columns?</p>
  
  <p>I can get the data formatted the way I would like in Powershell. it will display properly onscreen. All of the ways I have tried to export it do not provide the correct result.</p>
  
  <p>Also if possible, can background color, font, bold, etc be set from powershell and sent to the CSV file? Future scripts that I will have will be creating CSV files with 50+ columns and over 500 rows.</p>
  
  <pre><code># Setting Time Variable
  $Script_Time = (Get-Date -Format MM-dd-yyyy--HH-mm)
  
  # XXXXXXXX Path mapping for Computer Names, Ping Logs, CSV Name
  $Root_Path = $env:USERPROFILE + "\xxxx-Working-Path----\"
      $Computer_Names_Path = $root_Path + "Computer_Names\"
          $Computer_Names_File = $Computer_Names_Path + "\ComputerNames_Working.txt"
      $Ping_Logs_Path  = $Root_Path + "Logs\Ping_Logs\"
          $Log_Csv_Name = $Ping_Logs_Path + "Ping Results--" + $Script_Time + ".csv"
  
  # Blanking/Setting needed variables
  $Path_Result    = ""
  $Ping_Result    = ""
  $Name           = ""
  $Machine_Status = ""
  $Total_Count    = 0
  $Online_Count   = 0
  $Offline_Count  = 0
  
  # Gathering Computer Names from text file
  $List_Of_Computers = Get-Content "$Computer_Names_File"
  
  # Array creation for CSV output
  $Results_Array = [System.Collections.ArrayList]@()
  
  # Testing to see if folders exist
  If (Test-Path $Computer_Names_Path){
      $Path_Result = "Computer Names Path Exists"
      Write-Output $Path_Result
      }   
      Else{
      $Path_Result = "Computer Names Path Does Not Exist"
      Write-Output $Path_Result
      Pause
      }
  
  If (Test-Path $Ping_Logs_Path){
      $Path_Result = "Ping Logs Csv Path Exists"
      Write-Output $Path_Result
      }        
      Else{
      $Path_Result = "Ping Logs Csv Path Does Not Exist"
      Write-Output $Path_Result
      Pause
      }
  
  # Loop to gather online status of each machine
  Foreach ($Name in $List_Of_Computers) {
  
      # Test for True result
      If (test-connection -computername $name -buffersize 16 -count 1 -quiet){
          $Ping_Result = "Online"
          $Online_Count = $Online_Count + 1
          }
  
          # Test for false result
          Else{
          $Ping_Result = "Offline"
          $Offline_Count = $Offline_Count + 1
          }
  
          # Displays Online status and adds to CSV Array
          $Machine_Status = $Name + "," + $Ping_Result + "," + (Get-Date -Format MM-dd-yyyy--HH-mm-ss)
          Write-Output $Machine_Status
          [void]$Results_Array.add($Machine_Status)
  }
  
  # Total machine count
  $Total_Count = $Results_Array.Count
  
  # Adding Information to Top 2 rows in Array
  $Results_Array.Insert(0,"No of Machines: $Total_Count,Online Total: $Online_Count,Offline Total: $Offline_Count")
  $Results_Array.Insert(1,"Computer Name,Online Status,Timestamp")
  
  # Sends Array to CSV File
  set-content $Log_Csv_Name -Value $Results_Array
  </code></pre>
  
  <p>EDIT</p>
  
  <p>Applied what I had to a Excel Application output. Thanks for pointing my in that direction. It is working perfectly. Just need to finish fine tuning the formatting that I desire and learn how to save it with Powershell. Both, I assume, I will be able to research easily.</p>
  
  <p>Here is the new script I have come up with after researching and applying what I found. Hopefully it will help others if they stumble upon this.</p>
  
  <pre><code># Setting Time Variable
  Write-Output "Setting Time Variable" ""
  $Script_Time = (Get-Date -Format MM-dd-yyyy--HH-mm)
  
  # XXXXXXXX Path Mapping for Computer Names and Ping Logs
  Write-Output "Setting Path Variable" ""
  $Root_Path = $env:USERPROFILE + "\XXXXXXXXXXX\"
      $Computer_Names_Path = $root_Path + "Computer_Names\"
          $Computer_Names_File = $Computer_Names_Path + "\ComputerNames_Working.txt"
      $Ping_Logs_Path  = $Root_Path + "Logs\Ping_Logs\"
  
  # Testing to see if Folders Exist
  Write-Output "Testing to see if Folder Paths exist" ""
  If (Test-Path $Computer_Names_Path){
      $Path_Result = "Computer Names Path Exists"
      Write-Output $Path_Result
      }   
  Else{
      $Path_Result = "Computer Names Path Does Not Exist"
      Write-Output $Path_Result
      Pause
  }
  
  If (Test-Path $Ping_Logs_Path){
      $Path_Result = "Ping Logs Csv Path Exists"
      Write-Output $Path_Result ""
      }        
  Else{
      $Path_Result = "Ping Logs Csv Path Does Not Exist"
      Write-Output $Path_Result
      Pause
  }
  
  # Creating Excel Application
  Write-Output "Creating Excel and Setting up the Sheet" ""
  $New_Excel = New-Object -ComObject Excel.Application
  
  # Makes Excel Visable
  $New_Excel.Application.Visible = $true
  $New_Excel.DisplayAlerts = $false
  
  # Creating Excel WorkBook
  $New_Book = $New_Excel.Workbooks.Add()
  
  # Sets the Worksheet and Names it
  $New_Sheet = $New_Book.Worksheets.Item(1)
  $New_Sheet.name = "Ping Results--" + (Get-Date -Format MM-dd-yyyy--HH-mm)
  
  # Selects the Worksheet
  $New_Sheet.Activate()
  
  # Blanking/Setting Needed Variables
  Write-Output "Setting Start Variables" ""
  $Path_Result    = ""
  $Ping_Result    = ""
  $Name           = ""
  $Machine_Status = ""
  $Total_Count    = 0
  $Online_Count   = 0
  $Offline_Count  = 0
  $Row            = 1
  $Column         = 1
  
  # Creates the Title for the Sheet
  Write-Output "Creating Title" ""
  $New_Sheet.Cells.Item($Row,$Column) = "Ping Results"
  $New_Sheet.Cells.Item($Row,$Column).Font.Size = 21
  $New_Sheet.Cells.Item($Row,$Column).HorizontalAlignment = -4108
  $New_Sheet.Cells.Item($Row,$Column).Font.Bold = $true
  
  # Merging Cells for the Title
  $New_Sheet.Range("A1:c1").Merge()
  
  # Skipping Two Rows to leave space for Totals to be added at the end
  $row = $Row + 2
  
  # Creating Column Headers for the Data
  Write-Output "Creating Column Headers" ""
  $New_Sheet.Cells.Item($Row,$Column) = "Computer Name"
  $New_Sheet.Cells.Item($Row,$Column).Font.Size = 16
  $New_Sheet.Cells.Item($Row,$Column).Font.ColorIndex = 1
  $New_Sheet.Cells.Item($Row,$Column).Font.Bold = $True
  $column++
  $New_Sheet.Cells.Item($Row,$Column) = "Online Status"
  $New_Sheet.Cells.Item($Row,$Column).Font.Size = 16
  $New_Sheet.Cells.Item($Row,$Column).Font.ColorIndex = 1
  $New_Sheet.Cells.Item($Row,$Column).Font.Bold = $True
  $column++
  $New_Sheet.Cells.Item($Row,$Column) = "Timestamp"
  $New_Sheet.Cells.Item($Row,$Column).Font.Size = 16
  $New_Sheet.Cells.Item($Row,$Column).Font.ColorIndex = 1
  $New_Sheet.Cells.Item($Row,$Column).Font.Bold = $True
  
  # Sets the Column and Row for the First Set of Data
  $Row = $Row + 1
  $Column = 1
  
  # Gathers the List of Computers
  Write-Output "Gathering Computer Names" ""
  $List_Of_Computers = Get-Content "$Computer_Names_File"
  
  # Loop to get the status of each machine
  Write-Output "Gathering Data from each Computer" ""
  Write-Output "Computer Name     Online Status          Timestamp"
  Foreach ($Name in $List_Of_Computers){
      If (test-connection -computername $name -buffersize 16 -count 1 -quiet){
          $Ping_Result = "Online"
          $Online_Count = $Online_Count + 1
          }
      Else{
          $Ping_Result = "Offline"
          $Offline_Count = $Offline_Count + 1
          }
  
      $Machine_Status = $Name + "        " + $Ping_Result + "         " + (Get-Date -Format MM-dd-yyyy--HH-mm-ss)
      Write-Output $Machine_Status
  
      $New_Sheet.Cells.Item($row,$column) = $Name
      $column = $Column + 1
  
      $New_Sheet.Cells.Item($row,$column) = $Ping_Result
      $Column = $Column + 1
  
      $New_Sheet.Cells.Item($row,$column) = (Get-Date -Format MM-dd-yyyy--HH-mm-ss)
      $Row = $row + 1
      $Column = 1
  }
  
  # Adding Totals Information to Row 2
  $New_Sheet.Cells.Item(2,1) = "# of Machines: " + $List_Of_Computers.Count
  $New_Sheet.Cells.Item(2,2) = "Online Total: " + $Online_Count
  $New_Sheet.Cells.Item(2,3) = "Offline Total: " + $Offline_Count
  </code></pre>