Introduction

Yesterday I had the brilliant idea to automate the checkout of a git branch. As you might know, when you work with Visual studio and git, you need to close VS before you do a checkout and then you have to restart it. For the people that don’t know, Visual studio will start asking to reload your projects one at a time which can take many clicks, it is also possible that if you leave a certain tab open with a file that is not in the other branch that this tab remains open and if you do save all you might end up with this file in the wrong branch. All this seemed ideal for a little script. Little did I know the trouble I would get myself into, but I will report about this in my next post.

The beginning

First thing to do was to find the open devenv processes.

This is simple

Get-Process devenv ```
OK it isn’t. Get-process devenv will give you either an object of type process or an array of processes, depending if you have one or more processes of type devenv. This does not make for easy programming but I can live with it. 

So if you do this

$processes = Get-Process devenv write-host $processes.Count``` you will get no output if you have one devenv open or a number if you have several open. You will get an exception when there is no devenv.

So lets move on and imagine there are several devenv’s open we can do something like this to get a list of devenv’s.

if($processes.Count -gt 0)
{
    $i = 0
    Write-host "There are multiple processes for devenv."
    foreach($process in $processes)
    {
        $i++
        $i.ToString() + '. ' + $process.MainWindowTitle
    }
}```
Which gave me this.

> **2
  
> There are multiple processes for devenv.
  
> 1. ConsoleApplication1 – Microsoft Visual Studio (Administrator)
  
> 2. ConsoleApplication2 – Microsoft Visual Studio (Administrator)**

Then I wanted to pick one of the two processes so I could close and reopen just the one I was working on. Or if there is just one than I can just close and reopen that.

Next step is to take input.

Taking input is easy with read-host.

$in = Read-host “Give a number of the process to kill: “``` and now I can just pick the process I want.

write-host "killing and restarting: " + $processes[$in-1].MainWindowTitle
    ```
That has the desired result.

killing and restarting: + ConsoleApplication1 - Microsoft Visual Studio (Administrator)

Stopping the process is fairly easy.

$processes[$in-1].Kill() $processes[$in-1].WaitForExit()

That is the more friendly version of kill, you can also force it to kill in a less friendly manner.

So I’m nearly there I Guess. Now I just have to start the process up again.

That should be as simple as this.

$processes[$in-1].Start()``` Nope it isn’t since that

Exception calling “Start” with “1” argument(s): “Cannot start process because a file name has not been provided.”

I could now start to explain what I tried to get this going but it would take to long. I even posted on StackOverflow and asked the help of Aaron Nelson (blog|twitter) and I got close but not close enough.

The big problem is is that the process does not know what project/solution devenv has open so I can’t tell it to open that. I can tell it to open devenv again but that is not what I am after.

Conclusion

I failed (lucky I’m not a samurai else I would have had to commit suicide, although not everybody will think so). I could not get it to work in a reasonable time so I gave up on it, perhaps if I dig around some more I could find a possibility but I’m pretty sure that it is not worth it. But I had fun and learned a lot.