title: Powershell - Remove last comma in foreach
body: <p>I am rather new to powershell scripting and need help with my script below. I need to remove the last comma "," from the result below. This is JSON format that I need to be able to use this info for Zabbix monitoring of Hyper-V VMs.</p>
  
  <p>script:</p>
  
  <pre><code>    $colItems = Get-VM
  
  write-host "{"
  write-host " `"data`":["
  write-host
  
  foreach ($objItem in $colItems) {
      $line =  " { `"{#VMNAME}`":`"" + $objItem.Name + "`" , `"{#VMSTATE}`":`"" + $objItem.State + "`" },"
      write-host $line
  }
  
  write-host
  write-host " ]"
  write-host "}"
  write-host
  </code></pre>
  
  <p>Here is the result from this:</p>
  
  <pre><code>{ 
   "data":[
  
   { "{#VMNAME}":"cp01" , "{#VMSTATE}":"Off" },
   { "{#VMNAME}":"dc01" , "{#VMSTATE}":"Running" },
   { "{#VMNAME}":"ex01" , "{#VMSTATE}":"Off" },
   { "{#VMNAME}":"fc01" , "{#VMSTATE}":"Running" },
   { "{#VMNAME}":"rdg01" , "{#VMSTATE}":"Running" },
   { "{#VMNAME}":"Windows 2012R2 G2 Template" , "{#VMSTATE}":"Off" }, &lt;--- THIS I WANT TO REMOVE
  
   ]
  }
  </code></pre>