title: Adding groups via Script in powershell Forms.ListView
body: <p>I need adding a group of items in ListView but <code>$listView1.groups.add("groupName")</code> does not work !</p>
  
  <pre><code>$button1.add_click({
      # add group
          $listView1.groups.add("groupName") # Not Work
      # add items
      1..3 | %{
          $listView1.items.add("Item : $_") # work fine
      }
  })
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/H0YNu.png" alt="enter image description here"></p>
  
  <p>full code</p>
  
  <pre><code>#requires -version 2
  Add-Type -AssemblyName 'System.Drawing'
  Add-Type -AssemblyName 'System.Windows.Forms'
  
  #region $form1
  $form1 = New-Object -TypeName 'System.Windows.Forms.Form'
  $form1.SuspendLayout()
  
      #region $listView1
      $listView1 = New-Object -TypeName 'System.Windows.Forms.ListView'
      $listView1.Dock = [System.Windows.Forms.DockStyle]::Fill
      $listView1.View = [System.Windows.Forms.View]::Tile
      $listView1.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList @(250, 200)
      $listView1.BeginUpdate()
  
          #region $listViewGroup1
          $listViewGroup1 = New-Object -TypeName 'System.Windows.Forms.ListViewGroup'
          $listViewGroup1.Header = 'Group 1'
  
              #region $listViewItem1
              $listViewItem1 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
              $listViewItem1.Text = 'Item 1'
              #endregion $listViewItem1
  
              $listViewItem1.Group = $listViewGroup1
  
              #region $listViewItem2
              $listViewItem2 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
              $listViewItem2.Text = 'Item 2'
              #endregion $listViewItem2
  
              $listViewItem2.Group = $listViewGroup1
  
          #endregion $listViewGroup1
  
          [System.Void]$listView1.Groups.Add($listViewGroup1)
          [System.Void]$listView1.Items.Add($listViewItem1)
          [System.Void]$listView1.Items.Add($listViewItem2)
  
          #region $listViewGroup2
          $listViewGroup2 = New-Object -TypeName 'System.Windows.Forms.ListViewGroup'
          $listViewGroup2.Header = 'Group 2'
  
              #region $listViewItem3
              $listViewItem3 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
              $listViewItem3.Text = 'Item 3'
              #endregion $listViewItem3
  
              $listViewItem3.Group = $listViewGroup2
  
          #endregion $listViewGroup2
  
          [System.Void]$listView1.Groups.Add($listViewGroup2)
          [System.Void]$listView1.Items.Add($listViewItem3)
  
      $listView1.EndUpdate()
      #endregion $listView1
  
      [System.Void]$form1.Controls.Add($listView1)
  
      #region $button1
      $button1 = New-Object -TypeName 'System.Windows.Forms.Button'
      $button1.Dock = [System.Windows.Forms.DockStyle]::Bottom
      $button1.Text = 'Add Group'
      $button1.add_click({
        # add group
            $listView1.groups.add("groupName") # Not Work
        # add items
        1..3 | %{
            $listView1.items.add("Item : $_") # work fine
        }
      })
      #endregion $button1
  
      [System.Void]$form1.Controls.Add($button1)
  
  $form1.ResumeLayout($false)
  $form1.PerformLayout()
  #endregion $form1
  
  #region GUI Startup
  $form1.ShowDialog()
  #endregion GUI Startup
  </code></pre>