title: Reading 1000 line (350KB) XML file using Get-Content cmdlet takes 3minutes
body: <p>I wrote a script which uses regex to check if certain pattern exist in file. 
  The script reads the XML file line by line and checks if the pattern exists. If so, then it takes the line and adds it to the list. Afterwards it saves the list to another file.</p>
  
  <p>The script does its job, but is super slow. It takes around 3 minutes just to read through only 1000 lines of XML file. I have check the script at two machines.</p>
  
  <p>I have found out that <code>Get-Content</code> cmdlet is not much efficient for large files (50MB and more), but for small files like mine should be efficient enough.</p>
  
  <p>Here is the code I wrote (Windows 10, Powershell v5.1):</p>
  
  <pre class="lang-cs prettyprint-override"><code>$xmlFile = "C:\Dane\VSCrepo\Generator_Infa\impex\migrate\files_to_migrate\wf_TEST.xml"
  
  $TemplateFile = "C:\Dane\VSCrepo\Generator_Infa\impex\migrate\ControlFile_temp.xml"
  $OutputFile = "C:\Dane\VSCrepo\Generator_Infa\impex\migrate\ControlFile.xml"
  
  $xmlText = Get-Content $xmlFile
  
  # Read file
  [String[]] $NewLines = @() 
  foreach ($Line in $xmlText) {
      if ($Line -match '.*FOLDER *NAME ="\w+".*') {
          $FolderName = $Line -replace '.*FOLDER *NAME ="(\w+)".*', '$1'
          $NewLines += "&lt;FOLDERMAP SOURCEFOLDERNAME=`"$FolderName`" SOURCEREPOSITORYNAME=`"REP_DEV`" TARGETFOLDERNAME=`"$FolderName`" TARGETREPOSITORYNAME=`"REP_TEST`"/&gt;"
      }
  }
  
  $TemplateText = Get-Content $TemplateFile
  
  # Write file
  [String[]] $OutputText = @() 
  foreach ($Line in $TemplateText) {
      $OutputText += $Line
      if ($Line -match ".*&lt;IMPORTPARAMS CHECKIN.*") {
          #Add Lines after the selected pattern 
          $OutputText += $NewLines
      }
  }
  Set-Content $OutputFile $OutputText
  </code></pre>
  
  <p>Do you have any ideas if there is any easy method to improve performance of the code, without using .Net classes or another crazy things?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>XML file example:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;FOLDER NAME="XXXX" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
      &lt;blah blah blah&gt;
      &lt;yeah yeah yeah&gt;
      &lt;SHORTCUT FOLDERNAME="AAA" FOO="BLAH" BAR="BLAH"/&gt; 
      &lt;.../&gt;
  &lt;/FOLDER&gt;
  &lt;FOLDER NAME="YYYYY" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
      &lt;blah blah blah&gt;
      &lt;yeah yeah yeah&gt;
      &lt;SHORTCUT FOLDERNAME="BBB" FOO="BLAH" BAR="BLAH"/&gt; 
      &lt;.../&gt;
  &lt;/FOLDER&gt;
  &lt;FOLDER NAME="ZZZZ" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
      &lt;blah blah blah&gt;
      &lt;yeah yeah yeah&gt;
      &lt;SHORTCUT FOLDERNAME="CCC" FOO="BLAH" BAR="BLAH"/&gt; 
      &lt;.../&gt;
  &lt;/FOLDER&gt;
  </code></pre>
  
  <p>Regex match <code>-match '.*FOLDER *NAME ="\w+".*'</code> picks the lines:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;FOLDER NAME="XXXX" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
  &lt;SHORTCUT FOLDERNAME="AAA" FOO="BLAH" BAR="BLAH"/&gt; 
  &lt;FOLDER NAME="YYYYY" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
  &lt;SHORTCUT FOLDERNAME="BBB" FOO="BLAH" BAR="BLAH"/&gt; 
  &lt;FOLDER NAME="ZZZZ" GROUP="" OWNER="idwbicc" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="a33b65ea-7e70-4b3c-8dff-e6987d013fd2"&gt;
  &lt;SHORTCUT FOLDERNAME="CCC" FOO="BLAH" BAR="BLAH"/&gt;
  </code></pre>
  
  <p>Regex replacement <code>-replace '.*FOLDER *NAME ="(\w+)".*', '$1'</code> takes folder name from picked:</p>
  
  <pre>
  XXXX
  AAA
  YYYYY
  BBB
  ZZZZ
  CCC
  </pre>
  
  <p>Each folder name is placed in:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;FOLDERMAP SOURCEFOLDERNAME="$FolderName" SOURCEREPOSITORYNAME="REP_DEV" TARGETFOLDERNAME="$FolderName" TARGETREPOSITORYNAME="REP_TEST"/&gt;
  </code></pre>
  
  <p>List of the newly created lines is inserted in the output XML file.</p>