Search code examples
visual-studiopowershellenvdte

Powershell EnvDTE Switch Visual Studio Session (Multiple Separate Solutions open)


Here is what I am trying to do... I have two separate Visual Studio solutions open, they are completely separate solutions, and I want it to remain that way. Solution 1 is what my powershell DTEObject is pointing to. I want to be able to switch to Solution 2, and open a file in that session. Here is the powershell code I am using to get the DTE object and executing the open file command:

$dteobj = [runtime.interopservices.marshal]::getactiveobject('VisualStudio.DTE')
$dteobj.ExecuteCommand("Open ""$file""")
$dteobj.ExecuteCommand("Edit.Goto $line")

The reason why I want to switch solutions is because I want to then bring focus to the window of the solution that contains that file so that it doesn't confuse people about what files are in what solutions. Hopefully that makes enough sense.


Solution

  • I figured it out. You can use the powershell command Get-RunningObject from the PSCX module, and you have to select that object's DTE

    $dteObj = Get-RunningObject | Where-Object {$_.FullName -eq "$solutionDir$solutionName"} | Select-Object DTE
    $dteobj.DTE.ExecuteCommand("Open ""$file""")
    $dteobj.DTE.ExecuteCommand("Edit.Goto $line")