Get Assembly Version using PowerShell in Build Process

Team Foundation Services (TFS) by Microsoft generates its own internal build numbers that are unrelated to the product version you have setup for continuous integration. Fortunately, PowerShell can be used to execute a script on the build agent to retrieve the assembly version and then store the result in a variable that can be used for the rest of the build process.

As a case-example, say that as part of build process, you wanted to create a ZIP archive file that would then be uploaded to a server as part of your build or release cycle and that you would like the generated ZIP archive to contain the standard Microsoft version syntax.

A build process would look like the following:

Where the last few steps re:

  1. PowerShell script,
  2. Archive files,
  3. Upload.

The PowerShell script step has Type set to Inline Script, the Working folder in Advanced is set to $(Build.Repository.LocalPath)\MyProgram\bin where \MyProgram\bin is the path on the TFS Agent where the program binaries are generated.

Finally, the Inline Script is the following:

$AssemblyVersion = (Get-Item .\MyProgram.exe).VersionInfo.FileVersion
Write-Host "##vso[task.setvariable variable=AssemblyVersion]$AssemblyVersion"

where:

The script sets a VSO variable named "AssemblyVersion" that can then be referenced in ulterior steps. For instance, the Archive files build step in this example, will create a ZIP archive with Archive file to create set to $(Build.ArtifactStagingDirectory)\MyProgram-$(AssemblyVersion).zip.

This will generate a file such as MyProgram-10.23.8754.3424.zip that can then be further published via your setup distribution services.