Word-Dokument mit Powershell bearbeiten
Hallo,
ich würde gerne ein Worddokument, welches ich mit Variablen versehen habe, durch Werte mit Powershell ersetzten.
Hatte mir das ganze so vorgestellt:
Es existiert auf jedem Server schon eine Datei Servername.docx, die Variable in dem docx-Dokument heißt #Servername# und wird $Servername ersetzt.
$Servername = "Home1"
(Get-Content .\$Servername.docx) | Foreach-Object {$_ -replace '#Servername#', '$Servername'} | Set-Content .\$Servername.docx
Das funktioniert soweit ohne Fehlemdung, nur wenn ich das Dokument öffnen möchte, ist es leer.
Könnt ihr mir bitte helfen?
Mfg
Peter
ich würde gerne ein Worddokument, welches ich mit Variablen versehen habe, durch Werte mit Powershell ersetzten.
Hatte mir das ganze so vorgestellt:
Es existiert auf jedem Server schon eine Datei Servername.docx, die Variable in dem docx-Dokument heißt #Servername# und wird $Servername ersetzt.
$Servername = "Home1"
(Get-Content .\$Servername.docx) | Foreach-Object {$_ -replace '#Servername#', '$Servername'} | Set-Content .\$Servername.docx
Das funktioniert soweit ohne Fehlemdung, nur wenn ich das Dokument öffnen möchte, ist es leer.
Könnt ihr mir bitte helfen?
Mfg
Peter
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 312075
Url: https://administrator.de/forum/word-dokument-mit-powershell-bearbeiten-312075.html
Ausgedruckt am: 30.04.2025 um 04:04 Uhr
5 Kommentare
Neuester Kommentar

Hi
You have to use COM-Automation to open the document with word and then do the replacement .
Regards
das funktioniert soweit ohne Fehlemdung, nur wenn ich das Dokument öffnen möchte, ist es leer.
This will never work!! A word document is not plain text, instead it's a binary zip-file.You have to use COM-Automation to open the document with word and then do the replacement .
Regards

If you have not installed Word, COM-Automation will not work, then you have to unzip the document, replace it in the document xml content file, replace the file in the zip and save. Much more work and prone to errors.
Hallo Peter,
über COM-Automation sieht das bspw. so aus:
Wie man Office-Dokumente offline ohne installiertes Word bearbeiten kann kannst du in einem meiner Threads nachlesen
Pfad der Dokumentenvorlage mit einem Powershell Script ändern?
Grüße Uwe
über COM-Automation sieht das bspw. so aus:
$placeholder = "#SERVERNAME"
$replacement = "ServerXYZ"
$objWord = New-Object -Com Word.Application
$objWord.Visible = $false
$doc = $objWord.Documents.Open("C:\daten\serverxyz.docx")
$objWord.Selection.Find.Execute($placeholder,$false,$false,$false,$false,$false,$true,1,$false,$replacement,2)
$doc.Save()
$doc.Close()
$objWord.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($objWord)
Pfad der Dokumentenvorlage mit einem Powershell Script ändern?
Grüße Uwe
Hier noch als Ergänzung die Replacement-Methode ohne vorhandenes Word (Variablen im Kopf):
Falls der Beitrag gefällt, seid so nett und unterstützt mich durch eine kleine Spende / If you like my contribution please support me and donate
# Benötigt wird mindestens NET-Framework 4.5 und Powershell 3.0
# Ersetzungsteile
$placeholder = "#SERVERNAME#"
$replacement = "SERVERXYZ"
# Datei
$filespec = "C:\daten\demo.docx"
# benötigte Assemblies laden
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.Filesystem
gci $filespec -File | %{
# Word-Dokument als ZIP-Datei im Update-Modus öffnen
$zipfile = [System.IO.Compression.ZipFile]::Open($_.FullName,[System.IO.Compression.ZipArchiveMode]::Update)
# Einträge der benötigte Dateien aus dem Dokument holen
$entries = $zipfile.Entries | ?{$_.FullName -match '^(word/document\.xml|word/(header|footer)\d*\.xml)$'}
$remove = @()
$entries | %{
$tmp = "$env:TEMP\$($_.Name)"
# word/document.xml extrahieren
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_,$tmp)
$content = gc $tmp
if ($content -match [regex]::Escape($placeholder)){
# Einträge ersetzen
$content -replace [regex]::Escape($placeholder),$replacement | Set-Content $tmp -Encoding UTF8
# geänderte Datei wieder hinzufügen
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zipfile,$tmp,$_.FullName) | out-null
$remove += $_
}
# Temporäre Files löschen
del $tmp -Force -ErrorAction SilentlyContinue
}
# lösche ersetzte Dokumentteile im ZIP
$remove | %{$_.Delete()}
# Zipfile-Resourcen freigeben
$zipfile.Dispose()
}