DEV Community

Rich
Rich

Posted on • Originally published at yer.ac on

6 2

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..

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay