title: How to split ics calendar events in Powershell
body: <p>I'm trying to write a script to merge two Google calendar ics files and remove duplicate events. Using Powershell I tried the following</p>
  
  <pre><code>$cal1 = get-content ".\googlecalendar.ics"
  $calarr = $cal1 -replace ("BEGIN.VEVENT","###EVENTDELIM###`r`nBEGIN.VEVENT") -split ("###EVENTDELIM###")
  $calarr[4]
  </code></pre>
  
  <p>$calarr[4] produces METHOD:PUBLISH</p>
  
  <p>However, this produces an array of the line by line strings of the file. I am expecting each object in the array to be a multi-line string.</p>
  
  <p>I also tried another solution using regex like this.</p>
  
  <pre><code>$cal1 = get-content ".\googlecalendar.ics"
  $calarr = ([Regex]'BEGIN:VEVENT([\s\S]*?)END:VEVENT').Matches($cal1).Value
  $calarr[4]
  </code></pre>
  
  <p>$calarr[4] produces BEGIN:VEVENT DTSTART:20090929T220000Z DTEND:20090929T230000Z DTSTAMP:20180326T211259Z UID:dfjkadfawerjj14543@google.com CREATED:20090929T204053Z DESCRIPTION: LAST-MODIFIED:20091014T094735Z LOCATION: SEQUENCE:0 STATUS:CONFIRMED SUMMARY:intv TRANSP:OPA
  QUE END:VEVENT</p>
  
  <p>But that solution garbles up the individual line endings. Each key and value pair should be on their own line.</p>
  
  <p>What is the proper way to do sub-string selections in Powershell then to cast them into an array while both preserving non-printing characters and also the sub-string headers?</p>