Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Last active November 3, 2022 22:01
Show Gist options
  • Save bcnzer/f8d50699c5bf7614923bff733662cb1a to your computer and use it in GitHub Desktop.
Save bcnzer/f8d50699c5bf7614923bff733662cb1a to your computer and use it in GitHub Desktop.
Powershell script for generating a version number and inserting it into the csproj
$csprojPath = $args[0] # path to the file i.e. 'C:\Users\ben\Code\csproj powershell\MySmallLibrary.csproj'
$newVersion = $args[1] # the build version, from VSTS build i.e. "1.1.20170323.1"
Write-Host "Starting process of generating new version number for the csproj"
$splitNumber = $newVersion.Split(".")
if( $splitNumber.Count -eq 4 )
{
$majorNumber = $splitNumber[0]
$minorNumber = $splitNumber[1]
$revisionNumber = $splitNumber[3]
# I need to keep my build number under the 65K int limit, hence this hack of a method
$myBuildNumber = (Get-Date).Year + ((Get-Date).Month * 31) + (Get-Date).Day
$myBuildNumber = $majorNumber + "." + $minorNumber + "." + $myBuildNumber + "." + $revisionNumber
$filePath = $csprojPath
$xml=New-Object XML
$xml.Load($filePath)
$versionNode = $xml.Project.PropertyGroup.Version
if ($versionNode -eq $null) {
# If you have a new project and have not changed the version number the Version tag may not exist
$versionNode = $xml.CreateElement("Version")
$xml.Project.PropertyGroup.AppendChild($versionNode)
Write-Host "Version XML tag added to the csproj"
}
$xml.Project.PropertyGroup.Version = $myBuildNumber
$xml.Save($filePath)
Write-Host "Updated csproj "$csprojPath" and set to version "$myBuildNumber
}
else
{
Write-Host "ERROR: Something was wrong with the build number"
}
@JoeBrockhaus
Copy link

JoeBrockhaus commented Jul 21, 2017

I came across your script over here but ran into an issue in the 1.* PowerShell Script Task (I am running it Inline). It kept tossing an error that PropertyGroup did not contain a Property named Version. This threw me because it has no problem getting the value from $xml.Project.PropertyGroup.Version when it exists.

I ended up using XPath to get the node and set the InnerText:

$xml=[xml](get-content $src)
# just to ensure it actually exists. 
if ($xml.Project.PropertyGroup.Version -eq $null) { $xml.Project.PropertyGroup.AppendChild($xml.ImportNode(([xml]"<Version/>").DocumentElement,$true)) }
$xml.SelectNodes("/Project/PropertyGroup/Version")[0].InnerText = [version]($args[2])
$xml.Save($src)

@jakenuts
Copy link

jakenuts commented Oct 15, 2017

I really, really hate powershell.. Struggled forever with 'property does not exist' errors, so I punted and said "if it doesn't exist, perhaps it shouldn't (legacy cs proj format for one)..

function Set-CSProj-Version{
Param(
[parameter(Mandatory=$true)] [String] $filePath,
[parameter(Mandatory=$true)] [String] $newVersion
)

$xml=New-Object XML
$xml.Load($filePath)

if ($xml.Project.PropertyGroup.Properties.name -match "Version")
{
$xml.Project.PropertyGroup.Version = $newVersion
$xml.Save($filePath)
}
}

@bcnzer
Copy link
Author

bcnzer commented Nov 17, 2017

I updated the above script to create the version tag if it doesn't exist. Realized that if you have a new project and haven't changed the version number in the properties > package tab, the version tag won't exist in the XML doc. VS assumes that, since it doesn't exist, it's 1.0.0. I've also written a new blog post to show how to support .NET Core 2.0 and .NET Standard 2.0

@jtbrower
Copy link

jtbrower commented Feb 6, 2019

Thanks for posting this script, it saved me the time of writing it from scratch.

@aliegeni
Copy link

Changes not reflected on build process if script triggered from Pre-build event. MSBuild didn't notice *.csproj file changes on the fly. How to resolve that?

@tbremard
Copy link

tbremard commented Mar 2, 2022

$xml.Project.PropertyGroup might require index[0] to avoid set error because there might be several nodes PropertyGroup in your csproj

I use:
$xml.Project.PropertyGroup[0].FileVersion = $newVersion

on my project

@farshadpart
Copy link

God bless you ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment