Modifying Visual Studio Solution and Project Files with PowerShell

We are currently reorganizing the source code by moving the material to a new composition directory. This affects our Visual Studio solution and project files, where things like build links, possibly output directories, pre and post build events, etc ... need to be updated to reflect our changes.

Since we have many solutions and projects, I was hoping to partially automate the process using PowerShell, with something like a PowerShell provider for VS:

In an ideal world, I could do something like:

$MySolution.Projects["MyProject"].PostBuildEvent = "copy <this> to <that>" 

I know about PowerConsole (which I have not yet fully learned) for creating Visual Studio scripts. However, the documentation is not enough, and I'm not sure if it really covers my needs.

Anything else to easily manipulate solution and project files? Preferably in PowerShell, but I'm open to other suggestions.

+7
source share
1 answer

In my experience, the easiest way to manipulate Visual Studio solutions using PowerShell (inside or outside of Visual Studio) is to download the project file in XML format and use PowerShell to manage it.

 $proj = [xml](get-content Path\To\MyProject.csproj) $proj.GetElementsByTagName("PostBuildEvent") | foreach { $_."#text" = 'echo "Hello, World!"' } $proj.Save("Path\To\MyProject.csproj") 

If you use the script in the NuGet package management console, you can get the paths to all the project files, for example:

 PM> get-project -all | select -expand FileName C:\Users\Me\Documents\Visual Studio 10\Projects\MyProject\MyProject.csproj C:\Users\Me\Documents\Visual Studio 10\Projects\MyProject\MyProjectTests.csproj PM> 
+10
source

All Articles