title: Displaying charts in Excel using PowerShell
body: <p>I have read an input CSV file and generated a chart in PowerShell form.</p>
  
  <p>This is my code:
      $datasource = Import-Csv "D:\Users\janaja\Desktop\test.csv"
      [void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
      [void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')</p>
  
  <pre><code>$datasource = "D:\Users\janaja\Desktop\test.csv"
  $outputXLSX = "D:\Users\janaja\Desktop\test.xlsx"
  $excel = New-Object -ComObject excel.application 
  
  $xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
  
  
  $workbook = $excel.Workbooks.Add(1)
  $worksheet = $workbook.worksheets.Item(1)
  $TxtConnector = ("TEXT;" + $datasource)
  $Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
  $query = $worksheet.QueryTables.item($Connector.name)
  
  $query.TextFileOtherDelimiter = $Excel.Application.International(5)
  
  $query.AdjustColumnWidth = 1
  
  $query.Refresh()
  $query.Delete()
  
  $Workbook.SaveAs($outputXLSX,51)
  $i = 1 
  $j=1
  while($j -le 20){
  $excel.Cells.Item($i, $j).Font.ColorIndex = 3
  $excel.Cells.Item($i, $j).Font.Bold = $True 
  $j++
  }
  $Chart = New-Object -TypeName 
  System.Windows.Forms.DataVisualization.Charting.Chart
  $Chart.Size = '600,750'
  $ChartArea = New-Object -TypeName 
  System.Windows.Forms.DataVisualization.Charting.ChartArea
  $ChartArea.AxisX.Title = 'VM Name'
  $ChartArea.AxisY.Title = 'CPU and Memory Utilization'
  $ChartArea.AxisX.lnterval = '1'
  $ChartArea.AxisX.LabelStyle.Enabled = $true
  $ChartArea.AxisX.LabelStyle.Angle = 90
  $Chart.ChartAreas.Add($ChartArea)
  $Chart.Series.Add('Memory')
  $Chart.Series.Add('CPU')
  $Chart.Series['Memory'].ChartType = 
  [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
  $Chart.Series['CPU'].ChartType = 
  [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
  
  Foreach($VM in $datasource)
  {
  $var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
  }
  
  Foreach($CPU in $datasource)
  {
  $var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
  }
  
  $Title = New-Object -TypeName 
  System.Windows.Forms.DataVisualization.Charting.Title
  $Chart.Titles.Add($Title)
  $Chart.Titles[O].Text = 'VM details'
  $Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor 
  [System.Windows.Forms.AnchorStyles]::Right -bor 
  [System.Windows.Forms.AnchorStyles]::Top -bor 
  [System.Windows.Forms.AnchorStyles]::Left
  $Form = New-Object Windows.Forms.Form
  $Form.Text= "VM Memory and CPU usage Chart"
  $Form.Width = 600
  $Form.Height = 600
  $Form.controls.add($Chart)
  $Form.Add_Shown({$Form.Activate()})
  $Form.ShowDialog()
  $Chart.Savelmage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")
  </code></pre>
  
  <p>This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form. 
  How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?</p>