此代码创建一些具有不同页眉和页脚的文件。我希望它们合并为一个并保留页眉和页脚我怎么能在powershell中做到这一点
$val=ls $dir
set-variable -name wdAlignPageNumberCenter -value 1 
foreach ($file in $val){
    $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)
    ****some code******
    $filedata = (get-content $filename)
    $Footer.Range.Text=$page
    $head="$abcd`t`tFile ID: $file"
    $Header.Range.Text = $head  
    $line_count = 0
    ***some code*******
    $savepath="D:\$file.docx"
    $Doc.SaveAs([ref]$savepath) 
    $Doc.Close()
}