Repository: microsoft/PowerToys
File: .pipelines/UpdateVersions.ps1 (PowerShell)

Code before:
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName 
    }
}

# Update Directory.Packages.props file
$propsFile = [System.IO.Path]::Combine($rootPath,"Directory.Packages.props")
if (Test-Path $propsFile) {
    $file = Read-FileWithEncoding -Path $propsFile
    $content = $file.Content
    if ($content -match '<PackageVersion Include="Microsoft.WindowsAppSDK"') {
        $newVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="' + $WinAppSDKVersion + '" />'
        $oldVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*" />'
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $propsFile -Content $content -Encoding $file.encoding
        Write-Host "Modified " $propsFile
    }
}

# Update .vcxproj files
Get-ChildItem -Path $rootPath -Recurse *.vcxproj | ForEach-Object {
    $file = Read-FileWithEncoding -Path $_.FullName
    $content = $file.Content
    if ($content -match '\\Microsoft.WindowsAppSDK.') {
        $newVersionString = '\Microsoft.WindowsAppSDK.' + $WinAppSDKVersion + '\'
        $oldVersionString = '\\Microsoft.WindowsAppSDK.[-.0-9a-zA-Z]*\\'
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName
    }
}

# Update .csproj files
Get-ChildItem -Path $rootPath -Recurse *.csproj | ForEach-Object {
    $file = Read-FileWithEncoding -Path $_.FullName
    $content = $file.Content
    if ($content -match 'PackageReference Include="Microsoft.WindowsAppSDK"') {
        $newVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="'+ $WinAppSDKVersion + '"'
        $oldVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*"'
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName 
    }
}

Update-NugetConfig


Review comment (suggestion):
Add this since one of vcxproj for cmdpal is not following by "\\" after the version, instead, it just ended.
Remove the "\\" will cause matching for those with .props or .targets, thus adding extra check for at least 1 digit.
@shuaiyuanxx

Code after:
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName 
    }
}

# Update Directory.Packages.props file
Get-ChildItem -Path $rootPath -Recurse "Directory.Packages.props" | ForEach-Object {
    $file = Read-FileWithEncoding -Path $_.FullName
    $content = $file.Content
    if ($content -match '<PackageVersion Include="Microsoft.WindowsAppSDK"') {
        $newVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="' + $WinAppSDKVersion + '" />'
        $oldVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*" />'
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName
    }
}

# Update .vcxproj files
Get-ChildItem -Path $rootPath -Recurse *.vcxproj | ForEach-Object {
    $file = Read-FileWithEncoding -Path $_.FullName
    $content = $file.Content
    if ($content -match '\\Microsoft.WindowsAppSDK.') {
        $newVersionString = '\Microsoft.WindowsAppSDK.' + $WinAppSDKVersion
        $oldVersionString = '\\Microsoft.WindowsAppSDK.(?=[-.0-9a-zA-Z]*\d)[-.0-9a-zA-Z]*'    #positive lookahead for at least a digit
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName
    }
}

# Update .csproj files
Get-ChildItem -Path $rootPath -Recurse *.csproj | ForEach-Object {
    $file = Read-FileWithEncoding -Path $_.FullName
    $content = $file.Content
    if ($content -match 'PackageReference Include="Microsoft.WindowsAppSDK"') {
        $newVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="'+ $WinAppSDKVersion + '"'
        $oldVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*"'
        $content = $content -replace $oldVersionString, $newVersionString
        Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding
        Write-Host "Modified " $_.FullName 
    }
}

Update-NugetConfig