How can I update AssemblyVersion number in AssemblyInfo.cs with build number of Azure DevOps via PowerShell? How can I update AssemblyVersion number in AssemblyInfo.cs with build number of Azure DevOps via PowerShell? powershell powershell

How can I update AssemblyVersion number in AssemblyInfo.cs with build number of Azure DevOps via PowerShell?


The best way to do it is with Assembly Info Extension, and use the variable $(Build.BuildNumber) in the version field.

But if you want to use your own PowerShell script you can do it with this script:

$buildNumber = "$env:Build_BuildNumber"$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'$AssemblyFiles = Get-ChildItem . AssemblyInfo.cs -recforeach ($file in $AssemblyFiles){(Get-Content $file.PSPath) | ForEach-Object{    if($_ -match $pattern){        '[assembly: AssemblyVersion("{0}")]' -f $buildNumber    } else {        # Output line as is        $_    }} | Set-Content $file.PSPath}


I recommend installing the GitVersionTask nuget package - this will generate a version number based on the number of commits since your previous build automatically, and update AssemblyVersion and the BuildNumber for you.

https://github.com/GitTools/GitVersion