Running the Clean, Build and Rebuild through the Package Manager Console

nuget, powershell, visual-studio

Solution

Visual Studio's object model provides a way to build the entire solution or a single project via the SolutionBuild object.

Building the solution is straightforward from the NuGet Package Manager Console.

$dte.Solution.SolutionBuild.Clean($true)
$dte.Solution.SolutionBuild.Build($true)

The $true flag indicates that the command should wait for the clean/build to finish.

Building an individual project is not as straightforward. The SolutionBuild object provides a BuildProject method which takes three parameters.

$project = Get-Project | select -First 1
$dte.Solution.SolutionBuild.BuildProject("Debug", $project.FullName, $true)

It also does not allow you to run a Clean build.

If you want to build an individual project then, as Pavel suggests, using MSBuild seems to be more straighforward.

Problem

I want to run the Clean, Build and Rebuild commands through the Package Manager Console in Visual Studio but so far I haven't been able to find how. The following command gets me the first project inside the solution: ``` $project = Get-Project | select -First 1 ``` When I run the `$project | Get-Member`, I can see the members of `$project` item. ``` #Members of the $project ($project | Get-Member) # TypeName: System.__ComObject#{866311e6-c887-4143-9833-645f5b93f6f1} # #Name MemberType Definition #---- ---------- ---------- #ProjectName CodeProperty System.String ProjectName{get=GetCustomUniqueName;} #Delete Method void Delete () #Save Method void Save (string) #SaveAs Method void SaveAs (string) #Extender ParameterizedProperty IDispatch Extender (string) {get} #CodeModel Property CodeModel CodeModel () {get} #Collection Property Projects Collection () {get} #ConfigurationManager Property ConfigurationManager ConfigurationManager () {get} #DTE Property DTE DTE () {get} #ExtenderCATID Property string ExtenderCATID () {get} #ExtenderNames Property Variant ExtenderNames () {get} #FileName Property string FileName () {get} #FullName Property string FullName () {get} #Globals Property Globals Globals () {get} #IsDirty Property bool IsDirty () {get} {set} #Kind Property string Kind () {get} #Name Property string Name () {get} {set} #Object Property IDispatch Object () {get} #ParentProjectItem Property ProjectItem ParentProjectItem () {get} #ProjectItems Property ProjectItems ProjectItems () {get} #Properties Property Properties Properties () {get} #Saved Property bool Saved () {get} {set} #UniqueName Property string UniqueName () {get} #Type ScriptProperty System.Object Type {get=switch ($this.Kind) {... ``` I am not sure if I can get to the Clean Build and Rebuild method through the `$project` item or if I should I directly run the msbuild by targeting the project path. Any idea?

Original source