Powershell script to batch-update AssemblyInfo.cs with new Version

I wrote this script to update all the AssemblyInfo.cs files in a Visual Studio solution, to have the same version number. This is the kind of thing I would have used Perl to do, in the past. But Powershell’s got this covered nicely.

[ 23 April 2008 – 1150am PST – I updated the script to deal with the AssemblyInfo.vb files in a VB project, and also to deal with version numbers that have less than 4 digits. Thanks for the suggestion, Jason! ]

Maybe someone can learn from this.

# SetVersion.ps1
# Set the version in all the AssemblyInfo.cs or AssemblyInfo.vb files in any subdirectory.
# usage:
# from cmd.exe:
# powershell.exe SetVersion.ps1
# from powershell.exe prompt:
# .\SetVersion.ps1
# last saved Time-stamp: <Wednesday, April 23, 2008 11:46:40 (by dinoch)>

function Usage
echo “Usage: “;
echo ” from cmd.exe: “;
echo ” powershell.exe SetVersion.ps1″;
echo ” “;
echo ” from powershell.exe prompt: “;
echo ” .\SetVersion.ps1″;
echo ” “;

function Update-SourceVersion
Param ([string]$Version)
$NewVersion = ‘AssemblyVersion(“‘ + $Version + ‘”)’;
$NewFileVersion = ‘AssemblyFileVersion(“‘ + $Version + ‘”)’;

foreach ($o in $input)
Write-output $o.FullName
$TmpFile = $o.FullName + “.tmp”

get-content $o.FullName |
%{$_ -replace ‘AssemblyVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’, $NewVersion } |
%{$_ -replace ‘AssemblyFileVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’, $NewFileVersion } > $TmpFile

move-item $TmpFile $o.FullName -force

function Update-AllAssemblyInfoFiles ( $version )
foreach ($file in “AssemblyInfo.cs”, “AssemblyInfo.vb” )
get-childitem -recurse |? {$_.Name -eq $file} | Update-SourceVersion $version ;

# validate arguments
$r= [System.Text.RegularExpressions.Regex]::Match($args[0], “^[0-9]+(\.[0-9]+){1,3}$”);

if ($r.Success)
Update-AllAssemblyInfoFiles $args[0];
echo ” “;
echo “Bad Input!”
echo ” “;
Usage ;



Comments (5)

  1. Nice work.

    Considering your regex patterns don’t specify <> or [] surrounding the attribute text you could easily extend this script to update both AssemblyInfo.vb and AssemblyInfo.cs just by making the lines referencing ".cs" a bit smarter.


  2. cheeso says:

    Good suggestion, Jason!  I updated the script …

  3. shashi says:

    I tried you script to update the assembly info , executed your script from the powershell 1 script window, but assembly info file  remains same without any changes into version.

    could you please tell me the step how can i execute your script

  4. Cheeso Hello, I want to leave my thanks, you really helped me with this script.

    But maybe I can also leave a small contribution, I had a problem with the version above, when used together with a version control system like Mercurial, the file encoding is not preserved and Mercurial now think it is a binary file.

    Adding the following lines before "move-item $TmpFile $o.FullName -force" command solves the problem.

        # Preserve UTF-8 encoding of AssemblyInfo tmp file

        $utf8Content = Get-Content $TmpFile -encoding utf8

        [System.IO.File]::WriteAllLines($TmpFile, $utf8Content)