DEV Community

Rich
Rich

Posted on • Originally published at yer.ac on

Pragmatically upgrading .net framework version for all projects with PowerShell

Do feel free to provide any comments/feedback to @TheRichCarey on Twitter

We had a situation where we needed to upgrade all the CSPROJ files in a solution to 4.8. The issue is that some of our solutions contain almost a hundred projects so a manual intervention would be prone to error. (plus we have multiple solutions to apply this against!)

Whilst there are a number of extensions that do this on the VS Marketplace, they seemed a little overkill for something that can surely be achieved in PowerShell? At the end of the day its a simple find-and-replace, right?

This will load the solution file, iterate over each CSPROJ referenced and then replace the current framework version with the one specified in $versionToUse. It will then overwrite the file.

Potential improvements

This met my needs fine, but could be improved for sure! Things that it could do better are:

  • Auto scan for .sln files.
  • Provide some form of report at the end
  • Auto-checkout for TFS or Git (Using git CLI or TFS CLI)

The post Pragmatically upgrading .net framework version for all projects with PowerShell appeared first on yer.ac | Adventures of a developer, and other things..

Top comments (2)

Collapse
 
mburszley profile image
Maximilian Burszley • Edited

Big yikes. Don't use regex to parse XML when we have tools natively available to do that! Here's a POC for a single csproj:

$version = 'v4.8'

$project = (Get-Content -Path *.sln) -cmatch '^Project'
$csproj = ($project -split ',')[1].Trim() -replace '"'

$xml = [xml](Get-Content -Path $csproj)
$xml.SelectSingleNode('//TargetFrameworkVersion').'#text' = $version

$xml.Save($csproj)
Collapse
 
yerac profile image
Rich • Edited

Good idea! This was adapted from an existing find/replace script and didn't even consider using the Xml tooling in PS! (Mostly as this was a one-off!)
If I was doing anything more advanced, that would definitely be a great idea.