Click here to Skip to main content
Click here to Skip to main content

Upgrading VS 2008 projects to VS 2010 projects with PowerShell script

, 12 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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:

$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:

$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
    }
}

Points of Interest

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)

Share

About the Author

Vivek Krishnamurthy
Architect
India India
No Biography provided

Comments and Discussions

 
QuestionNot an article PinmvpMark Nischalke12-Mar-12 2:02 
AnswerRe: Not an article PinmemberVivek Krishnamurthy13-Mar-12 16:55 
QuestionVS 2080 PinmemberAjay Vijayvargiya11-Mar-12 18:26 
Are you serious?
AnswerRe: VS 2080 PinmemberVivek Krishnamurthy11-Mar-12 18:52 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 12 Mar 2012
Article Copyright 2012 by Vivek Krishnamurthy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid