title: Different header on different pages of word doc file in powershell?
body: <p>i am creating a doc file after reading several text files and trying to add 
  text file name in header of page corresponding in the <strong>doc file</strong> in <strong>powershell</strong>
  but the code below write the last read file as header on each page.
  note:- data is displayed corected on doc file only header is not written correctly.
  How to write text file name as header on doc (means same doc will have different header on different pages)</p>
  
  <pre><code>$dir="D:\abcd"
  $val=ls $dir
  $file_count=(get-childitem .).count
  $page_count=0
  $hash=@{};
  set-variable -name wdAlignPageNumberCenter -value 1 
  $Word = New-Object -ComObject Word.Application
  $Word.Visible = $true
  $Doc = $Word.Documents.Add()
  $Section = $Doc.Sections.Item(1)
  $Header = $Section.Headers.Item(1)
  $Footer = $Section.Footers.Item(1)
  $Footer.PageNumbers.Add($wdAlignPageNumberCenter)
  $selection=$word.Selection
  $page="`tPage";
  $loop=1
  foreach ($file in $val){
      $filename = "D:\abcd\$file"
      $filedata = (get-content $filename);
      $Footer.Range.Text=$page
      $head="ABCD`t`tFile ID: $file"
      $Header.Range.Text = $head  
  }
  $savepath="D:\abcd\$file.docx"
  $Doc.SaveAs([ref]$savepath) 
  $Doc.Close()
  </code></pre>