Exploring WimgApi.dll with PowerShell

Place to discuss MicroWinpeBuilder
Post Reply
Noel-Blanc
Registered User
Posts: 163
Joined: Mon 07 Jun, 2021 9:04 am
Location: Nantes France

Exploring WimgApi.dll with PowerShell

Post by Noel-Blanc »

Hi,
I just asked the AI to structure my text: the result shows me that I still have a way to go to write a text that is pleasant to read. I am sharing with you the version restructured by the AI.

Motivation
When you retire, you need to keep yourself busy. For me, that means experimenting with WimgApi.dll—even if I haven’t managed to convince my wife of its charm.

I’ve long wanted to write a PowerShell script to explore the contents of a WIM file using Microsoft’s WimgApi.dll. This week’s exercise: could I extract a file contained in a WIM image?

Of course, there are existing programs that do this. But using them doesn’t teach me anything about my own abilities. At 70, I believe in keeping the neurons active through learning and experimentation. With patience and step by step progress, I achieved my goal.

Learning Process
Working with WimgApi.dll proved laborious. To make progress, two resources were essential:
  • The official API documentation from Microsoft.
  • The header file WimgApi.h, which provides the formal definitions and constants.
Documentation consulted
C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\SDKs\Wimgapi\Include\wimgapi.h

Microsoft Docs – WIM API (Windows 11)
https://learn.microsoft.com/en-us/windo ... windows-11

ManagedWimgApi project on GitHub
https://github.com/jeffkl/ManagedWimgAp ... /wimgapi.h

WIM file format documentation (libyal)
https://github.com/libyal/assorted/blob ... t.asciidoc

Legacy MSDN documentation (Vista)
https://learn.microsoft.com/en-us/previ ... dfrom=MSDN

Microsoft download page (Wim format)
https://www.microsoft.com/en-my/downloa ... x?id=13096

Key Challenges
Finding correct API signatures for C# Early references were for Windows 7/8, not Windows 10/11. Mixing them led to incompatibilities—parameters and handle positions differ between versions.

Understanding API sequences For example, you must set a temporary directory before opening a WIM image object. Without it, subsequent calls fail with error 203 (“bad environment”).

Interop work Writing the API declarations in C#, then adapting them for PowerShell. This mechanical work is made easier with AI assistance.

Remarks
This script is not final—it’s a presentation of experiments.

The WIM filename could be passed as a parameter, though I usually copy/paste snippets directly into the console.

TODO: verify proper release of handles and memory blocks.

Extracting a file requires an administrator console.

I added output about ReparsePoints, a topic that interests me. My current understanding: NTFS hardlinks (e.g., WinSxS) are unrelated to ISO size optimization (CDFS). Corrections welcome.

Conclusion
This exercise was both a technical challenge and mental gymnastics. I hope reading the script inspires you to modify it, improve it, and adapt it to your own needs.

a picture:
Wim3.png
Wim3.png (283.73 KiB) Viewed 17205 times
the script:
Get-WIM_Demo.txt.ps1.7z
(6.37 KiB) Downloaded 747 times
happy to see this site is in good health !
:bike:
Last edited by Noel-Blanc on Wed 21 Jan, 2026 1:37 pm, edited 2 times in total.
Knowledge increases when it is shared
User avatar
Homes32
Code Baker
Posts: 291
Joined: Sun 06 Jun, 2021 8:10 pm

Re: Exploring WimgApi.dll with PowerShell

Post by Homes32 »

years ago I undertook a similar endeavor when I wrote an AutoIT UDF for wimgapi.dll and a companion program (WimUtil) that was used by the VistaPE and Win7PE projects for many years until wimlib had matured enough to be feature complete with the old ImageX tool..

the Microsoft API documentation was the most helpful, as well as just the practice of testing everything. It got interesting when M$ made a major update that required tweaking a couple of calls depending on which wimgapi.dll the user had installed on their system.

I remember at the time I couldn't get APPLY (random hang/crash) to work reliably with Autoit3 due to what I guessed was some multi-threading that autoit was unable to deal with in the callback.
Noel-Blanc
Registered User
Posts: 163
Joined: Mon 07 Jun, 2021 9:04 am
Location: Nantes France

Re: Exploring WimgApi.dll with PowerShell

Post by Noel-Blanc »

Thank you, Homes32, for taking the time to have a dialogue.
It is very encouraging for me to exchange a few words on this site.
Sometimes beginners like me (I am self-taught with no solid technical foundation) need to experiment with their hands, without relying on programs that are already available.
Everyone progresses at their own pace. And I am quite behind.
I believe that anyone can make progress if they have access to the right technical information.
And sometimes sharing that information again, or repeating it in a different form, can offer new perspectives to beginners by demystifying certain technical points.
:bike:
Knowledge increases when it is shared
Post Reply