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 theString.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()
andSet-Content
.