title: How do I set the build number in Teamcity?
body: <p>I'm trying to set the build number format in Teamcity using Powershell (kinda like in this Octopus Deploy <a href="https://octopus.com/blog/teamcity-version-numbers-based-on-branches" rel="noreferrer">blog post</a>).</p>
  
  <p>However, I don't know if the way Teamcity handles the service messages has changed since then or if I'm doing this wrong because it's not working.</p>
  
  <p>I have the following Powershell script as the first step in my build config:</p>
  
  <pre><code>$buildCounter = "%build.counter%" 
  $buildConfig = "%system.buildconfig%"
  $version = "%system.majorMinorVersion%"
  
  $branch = "%vcsroot.branch%"
  
  if ($branch.Contains("/")) 
  {
      $branch = $branch.substring($branch.lastIndexOf("/")).trim("/")
  }
  
  $buildNumber = "${version}.${buildCounter}-${branch}"
  
  Write-Host "##teamcity[buildNumber '$buildNumber']"
  </code></pre>
  
  <p>(I've also tried <code>$branch = "%teamcity.build.branch%"</code> on line 5)</p>
  
  <p>So when I try this, the if statement doesn't run because although it seems like in the example given by Octopus Deploy that $branch is set to the actual value of <code>%vcsroot.branch%</code> it's not and the result that it writes out to Teamcity is literally <code>##teamcity[buildNumber '$buildNumber']</code>. However, this does seem to work but the build number can't have <code>/</code> in it so my build fails because the branch is set to <code>refs/head/master</code> rather than just <code>master</code>.</p>
  
  <p>Where am I going wrong?</p>