extract header or footer from word as a table (powershell)

powerswitch 41 Reputation points
2021-07-05T16:50:20.517+00:00

Hi dear colleagues,

i would request your help to get the fields of the header or footer in proper format.
The code is here:

$docPath = "C:\Users\Dummy\Desktop\"  
$outputfile = "C:\Users\Dummy\Desktop\header_out.csv"  
if(Test-Path -path $outputfile) { Remove-Item -path $outputfile }  
  
$all_docs = Get-ChildItem $docPath -filter "n.docx"  
$word = New-Object -comobject "Word.Application"  
$word.Visible = $False  
$all_items = @()  
  
foreach ( $document in $all_docs)  
{  
  
 $item = New-Object System.Object  
 $doc = $word.Documents.Open($document.FullName);  
   
 $header = $doc.Sections.Item(1).Headers.Item(1).Range.Text  
  
   
 $item | Add-Member -type NoteProperty -name Name -value $document.FullName  
 $item | Add-Member -type NoteProperty -name Header -value $doc.Sections.Item(1).Headers.Item(1).Range.Text  
 $all_items += $item  
 $doc.Close()  
   
}  
$word.Quit()  
Remove-Variable doc  
Remove-Variable word  
$all_items | Export-CSV $outputfile  

but the csv output returns all the data with the title of fields unrelated with his values like this

Confidentiality Class External Confidentiality Label Document Type Page Public Non restricted Instruction 0 (1) Prepared By (Subject Responsibl
e) Approved By (Document Responsible) Checked Powersitch Powerswitch boss Monday Document Number Revision Date Reference Number 32232 PA2 2021
-06-29 WPH-1

the searched format would be (like the table of the header) :

Confidentiality Class External Confidentiality Label Document Type Page
Public Non restricted Instruction 1 (1)

Prepared By (Subject Responsible) Approved By (Document Responsible) Checked
Powersitch Powerswitch boss Monday

Document Number Revision Date Reference
Number 32232 PA2 2021-06-29 WPH-1

111820-capture.png

edit: added a screenshot

please, could anybody help to reach this?

Thanks & BR to all !!!!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.