Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / PowerShell
Tip/Trick

Upgrading VS 2008 projects to VS 2010 projects with PowerShell script

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Mar 2012CPOL3 min read 21.4K   201   4   4
Power shell script for upgrading Visual Studio 2008 solutions and projects into Visual Studio 2010.

Introduction

I have put together the Power shell script to upgrade solutions from Visual studio 2008 to Visual studio 2010. The script does it in few steps. Migrate the solution files, then the project, upgrade the target version then save the project file.

This is one of the first few scripts in powershell, if you feel some things can be done better, please let me know.

Background

I recently had to migrate our codebase from Visual studio 2008 to Visual Studio 2010, then upgrade the target framework to .Net 4.0. Number of solutions is 100+, doing it manually one project at a time is very time consuming and honestly really boring. So this made me to try and automate most parts of this.

Using the code

The script consists of 2 sections:

1. Upgrade the solution

2. Modify the Target framework on the Project

Upgrade the solution

To upgrade the solution use the devenv.exe with /Upgrade command line switch.

Following is the power shell script that does it:

C++
$sourcePath = 
"D:\MigTest\Src"
$devenv = "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe"

$sourcePath -recurse -filter "*.sln" 
foreach ($i in $slnFiles)
{
# 
Upgrade visual stdio solution to visual studio 2010
& $devenv $i.FullName 
/Upgrade
Write-Host $i.FullName
}

The above script is run on all the sln files found in the input directory.

We have an alternate provision for upgrading the project files to visual studio 2010 using vcupgrade.exe. This would be available as part of Windows SDK 7. And helpful in case you like to perform the upgrade on the system which does not have visual studio installed. This tool comes with limitation that it can't convert solution files.

This script is using visual studio upgrade options and needs Visual studio 2010 installed on the machine its running on.

Modify the Project file

When we change the target framework from within visual studio these are the changes.

1. OldTargetFramework tag

2. TargetFrameworkVersion

3. ToolsVersion

4. Project element

Following power shell scripts can take care of this:

<pre>$sourcePath = "D:\MigTest\Src"

# if TargetFrameworkVersion is not already 4.0
if($toolsVersion.Value -ne "4.0")
{
    # Upgrade Project / ToolsVersion="4.0"
    $toolsVersion.Value = "4.0"
 $isProjectXmlUpdated = $true
}
    
# if TargetFrameworkVersion is not already 4.0
# Upgrade TargetFrameworkVersion "v4.0"
$targetFrameworkElements = $projectGroup.GetElementsByTagName("TargetFrameworkVersion")
$oldToolsVersion = "v3.5"
foreach($targetFrameworkElement in $targetFrameworkElements)
{
    $oldToolsVersion = $targetFrameworkElement.InnerText
    $targetFrameworkElement.InnerText = "v4.0"
    $isProjectXmlUpdated = $true
    break
}

# Update OldToolsVersion from TargetFrameworkVersion
$oldToolsVersionElements = $projectGroup.GetElementsByTagName("OldToolsVersion")
if($oldToolsVersionElements -ne $null -and $oldToolsVersionElements.Count -gt 0)
{
    foreach($oldToolsVersionElement in $oldToolsVersionElements)
    {
        $oldToolsVersionElement.InnerText = $oldToolsVersion
  $isProjectXmlUpdated = $true
        break
    }
}

</pre><h2>Points of Interest</h2>

Execution Policy

When you try to run the above script, you will end up in access violation, the current script execution policy wont allow execution of the scripts. You can check the current execution policy using Get-ExecutionPolicy command. You need to update the execution policy to allow the script to run, Set-ExecutionPolicy to Unrestricted if you fully trust the script you are running. You should set the earlier execution policy once you are done with the script.

Starting process

We can start a process using Start-Process command let or by using Start-Job or simply by invoking the .exe directly. Each one of these have its own advantages.

Launching an exe as below, power shell console starts this process and continues with the next command, it wont wait for the response. In case of this script, even after the migration activity show its complete. Some of the devenv processing could still potentially be running in the background. Once all the devenv process are completed only then the upgrade is complete.

$devenv = "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" 
&$devenv $fileName /Upgrade<code style="font-family: Calibri,sans-serif; font-size: 11pt;">  

The other alternate is to use Start-Proces command let which has an option to wait for the completion of the process.

$args = '$fileName /Upgrade' 
Start-Process $devenv $args -Wait  

XML Formatting

Once the project file is updated with Target framework and other xml elements, we need to write it back to the project file. the xml file not formatted anymore. To preserver or apply the prefered formatting we can use XmlTextWriter with correct Formatting and Indentation option.

    $StringWriter = New-Object System.IO.StringWriter 
    $XmlWriter = 
New-Object System.XMl.XmlTextWriter $StringWriter 
    $xmlWriter.Formatting 
= "indented" 
    $xmlWriter.Indentation = $Indent 
    
$xml.WriteContentTo($XmlWriter) 
    $XmlWriter.Flush() 
    
$StringWriter.Flush() 
    Write-Output $StringWriter.ToString() 

Further Updates

I am planning to updated the scripts with the following:

  • Use configuration file for inputs to the script such as folder for conversion, location of the Visual studio 2010.
  • Report for the upgrade in text or html format

References

Windows PowerShell Quick Reference

Microsoft Script Cente

Windows PowerShell Cheat Sheet

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot an article Pin
Not Active12-Mar-12 2:02
mentorNot Active12-Mar-12 2:02 
AnswerRe: Not an article Pin
Vivek Krishnamurthy13-Mar-12 16:55
Vivek Krishnamurthy13-Mar-12 16:55 
QuestionVS 2080 Pin
Ajay Vijayvargiya11-Mar-12 18:26
Ajay Vijayvargiya11-Mar-12 18:26 
AnswerRe: VS 2080 Pin
Vivek Krishnamurthy11-Mar-12 18:52
Vivek Krishnamurthy11-Mar-12 18:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.