How to extract attachments from InfoPath forms
Updated 11 October 2026
Many InfoPath forms carry the documents that matter most: signed scans, quotes, photos, spreadsheets. They are
not stored as separate files in SharePoint. InfoPath packed each one inside the form's XML, so
a form library that looks like a list of small .xml files can hold years of attached documents.
With InfoPath gone, here is how to get them out without it.
How InfoPath stores an attachment
A File Attachment field holds one long line of base64 text. Decoded, it is not the file itself: it starts with a header that InfoPath adds, followed by the file name and then the file's bytes. Microsoft documents this format in the InfoPath form file format specification. The parts are:
- 4 bytes: the signature
C7 49 46 41. In base64 every attachment therefore starts with the charactersx0lGQ, which is an easy way to spot them. - Five 4-byte numbers: header size, version, a reserved value, the file size and the length of the file name in characters (including a terminating zero).
- The file name in UTF-16, then the file's bytes.
That is why simply base64-decoding the field gives you a file that won't open: you have to skip the header and the name first.
A free PowerShell script
This script uses only PowerShell, which comes with Windows. It goes through every .xml form in a
folder and its subfolders, finds the attachment fields by their x0lGQ start, and saves each file
as <form number>_<form name>_<attachment number>_<original file name>, so
forms with the same name in different subfolders don't overwrite each other. A damaged attachment is reported and
skipped, never written. Save it as
Export-Attachments.ps1:
# Saves every file attachment found in the InfoPath forms (.xml) of a folder and its subfolders.
param([string]$Forms = ".", [string]$Out = ".\attachments")
$Out = (New-Item -ItemType Directory -Force -Path $Out).FullName
$invalid = [IO.Path]::GetInvalidFileNameChars()
$formNumber = 0
foreach ($file in Get-ChildItem -Path $Forms -Filter *.xml -Recurse) {
$formNumber++
$xml = New-Object System.Xml.XmlDocument
try { $xml.Load($file.FullName) } catch { "Skipped $($file.FullName): not a valid XML file"; continue }
$i = 0
foreach ($node in $xml.SelectNodes("//*[not(*)]")) {
$text = $node.InnerText -replace '\s', ''
# InfoPath attachments start with the bytes C7 49 46 41, which is "x0lGQ" in base64
if (-not $text.StartsWith("x0lGQ")) { continue }
$i++
try {
$bytes = [Convert]::FromBase64String($text)
$size = [BitConverter]::ToInt32($bytes, 16)
$nameChars = [BitConverter]::ToInt32($bytes, 20)
$start = 24 + $nameChars * 2
if ($nameChars -lt 1 -or $size -lt 0 -or $start + $size -gt $bytes.Length) { throw "damaged attachment header" }
$name = [Text.Encoding]::Unicode.GetString($bytes, 24, $nameChars * 2).TrimEnd([char]0)
foreach ($c in $invalid) { $name = $name.Replace([string]$c, "_") }
# The form number keeps names unique when two subfolders contain forms with the same name
$target = Join-Path $Out ("{0:D5}_{1}_{2}_{3}" -f $formNumber, $file.BaseName, $i, $name)
$stream = [IO.File]::Create($target)
try { $stream.Write($bytes, $start, $size) } finally { $stream.Dispose() }
"Saved $target"
} catch {
"Skipped attachment $i in $($file.FullName): $($_.Exception.Message)"
}
}
}
Run it from PowerShell, pointing it at a copy of your forms:
powershell -ExecutionPolicy Bypass -File .\Export-Attachments.ps1 -Forms C:\Archive\Forms -Out C:\Archive\Attachments
We tested it with Windows PowerShell 5.1 on Windows 11; on 30 publicly available templates filled with sample data, all 419 attachments came out byte-for-byte identical to our own decoder's output. Work on a copy of the forms: the script only reads them, but it is good practice with records.
What the script doesn't do
- Pictures. Picture fields that store the image inside the form are plain base64 without the
header above (they start with the image's own signature, such as
iVBORfor PNG). The script skips them on purpose. - Context. You get a folder of files named after the form, but not which field or which row of a repeating table each file came from.
- Very large forms. It loads each form and each attachment into memory, so forms with attachments of hundreds of megabytes need plenty of RAM.
- Everything else in the form. The values, the layout and a printable copy are separate problems: see how to open InfoPath forms without InfoPath.
Attachments as part of a full archive
If you are archiving a whole form library, XSN Archive Viewer extracts attachments and embedded pictures as part of the same run that produces a PDF of each form and CSV files with its data. Files go into one folder per form, with their original names, and each PDF shows the attachment's file name where the field was. It runs on Windows. Conversion runs on your PC; your forms are never uploaded.