Powershell Script to Update .NET version

Powershell Script to Update .NET version

TargetFramework/TargetFrameworks

·

1 min read

Recently had to update the .NET Framework version for a service. Initially, did a quick and dirty regex replace:

$projectPaths = Get-ChildItem -include *.csproj -Recurse -Path $Path
(Get-Content $_.FullName) -Replace '<TargetFramework(s)?>.*', $targetFrameworkTag | Set-Content $_.FullName

Here is a more robust script with an option to use XmlDocument and XPath :

Summary

  • This script can be updated to update other tags/properties.

  • Use the -replace parameter/flag that supports regular expressions as opposed to the String.Replace() which does not.

  • Double-check your regex pattern for anchors (specifically ^) when there shouldn't be in this scenario.

  • Ensure encoding is set to what you expect for both XmlDocument.Save() and Set-Content.

References