Language: Deutsch English

Last Update: 2022 - 10 - 29

# Creating WIX File Elements from a Visual Studio Project File Using PowerShell

by Philipp Stiefel, originally published 2021-08-04

last updated 2021-08-04

The NuGet package manager is a very convenient tool/platform to add third-party libraries to your .NET projects. With a few mouse clicks or command line commands you quickly add a library including all its dependencies to your project references. - Great!

However, if you distribute the output assembly of your project in an MSI installer package created with the WIX Toolset, you still have a lot of work to do. You need to add all the required files to your WIX .wxs file as XML File Elements. Even though I hate manual repetitive work, I used a manual copy,paste&adjust process to do this until now.

Last week I had to add all the files of the .NET client libraries for Azure DevOps to a WIX project. Those are 50+ file references. - I just couldn’t motivate myself to add those manually.

As all the file references were already in my Visual Studio project file, all the info that needed to be transferred to the .wxs file was available in a machine-readable format. – Just not the format I needed for WIX XML.

I quickly had the idea to use PowerShell to extract the references from the project file and transform them to WIX XML. – As I rarely work with PowerShell, I struggled for over an hour to find the required commands and to get the syntax right. Nevertheless, in the end the result turned out exactly as expected. By running a 7 lines PowerShell script I generated XML output, which I then could easily copy&paste into my WIX .wxs file and was done. ??

As I am going to forget how exactly this works until I need it the next time, I put it up here for Google to help me find it when I desperately enter search terms for it next time. – Of course, you are welcome to use it too.

[xml]$pj = Get-Content "C:\path\to\your\project.vbproj"$pj.Project.ItemGroup.Reference | ForEach-Object { If($_.HintPath -ne$null) { $fileName = [System.IO.Path]::GetFileName($_.HintPath) '<File Id="' + $fileName.Replace('.','_') + '" Name="' +$fileName + '" KeyPath="no" DiskId="1" Source="' + \$_.HintPath + '"/>' | Out-File -FilePath "C:\tmp\output.txt" -Append } }

As I rarely need this, I didn’t both to create a proper .ps1 script with arguments. You need to adjust the hardcoded paths inside the script and then just paste it into a PowerShell console window.

I used it with an VB.NET project, but I believe it will work all the same with a C# project.