Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying Visual Studio solution and project files with PowerShell

We are currently reorganizing our source code, moving stuff around in a new directory structure. This impacts our Visual Studio solution and project files, where things like assembly references, possibly output directories, pre and post build events, and so on ... must be updated to reflect our changes.

Since we have many solutions and projects, my hope was to partly automate the process using PowerShell, with something like a PowerShell "provider" for VS:

In an ideal world, I would be able to do something like:

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

I know about PowerConsole (which I haven't fully explored yet) for scripting Visual Studio. However, the documentation is scarce and I'm not sure it really covers my needs.

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

like image 829
David Brabant Avatar asked May 23 '12 10:05

David Brabant


1 Answers

In my experience, the easiest way to manipulate Visual Studio solutions using PowerShell (from within or outside of Visual Studio) is to load the project file as XML and use PowerShell to manipulate 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're running your script in the NuGet Package Manager Console, you can get the paths to all of the project files like so:

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>
like image 80
Damian Powell Avatar answered Oct 02 '22 08:10

Damian Powell