Damian Hickey

Mostly software and .NET related. Mostly.

Rename a Visual Studio Project using PowerShell

I seem to be mostly posting about PowerShell these days... but it's because I'm find all these neat ways to make my life easier.

Renaming a project within Visual Studio is easy enough. If, like me, your project folder name is the same as the project and you'd like that to be renamed to. Doing it directly in Windows Explorer will result in a couple of problems: the solution won't find the project so you have to remove and re-add it; projects that referenced the renamed project will have to have their references re-added.

And then your root namespace, assembly name, and assembly title need to be fixed too (assuming these are the same as the project name, which is usually the case).


The script below solve this problem. It:

  1. hg renames the project file
  2. hg renames the folder
  3. changes the assembly title in AssemblyInfo.cs
  4. changes the assembly root namespace in the project file
  5. changes the asssembly name in the project file
  6. updates all other .csproj file's reference path to the renamed project
  7. updates the path to the renamed project in the solution file
  8. is designed to be called from the same working folder as the .sln
  9. expects all project folders to be immediate child folders of the working folder

Note: This a definite 'works for me' script so will probably need some adjusting to work for you.

function Rename-Project
	# designed to run from the src folder
		[string]$projectName=$(throw "projectName required."),
		[string]$newProjectName=$(throw "newProjectName required.")
	if(!(Test-Path $projectName)){
		Write-Error "No project folder '$projectName' found"
	if(!(Test-Path $projectName\$projectName.csproj)){
		Write-Error "No project '$projectName\$projectName.dll' found"
	if((Test-Path $newProjectName)){
		Write-Error "Project '$newProjectName' already exists"
	# project
	hg rename $projectName\$projectName.csproj $projectName\$newProjectName.csproj
	# folder
	hg rename $projectName $newProjectName
	# assembly title
	$assemblyInfoPath = "$newProjectName\Properties\AssemblyInfo.cs"
	(gc $assemblyInfoPath) -replace """$projectName""","""$newProjectName""" | sc $assemblyInfoPath
	# root namespace
	$projectFile = "$newProjectName\$newProjectName.csproj"
	(gc $projectFile) -replace "<RootNamespace>$projectName</RootNamespace>","<RootNamespace>$newProjectName</RootNamespace>" | sc $projectFile
	# assembly name
	(gc $projectFile) -replace "<AssemblyName>$projectName</AssemblyName>","<AssemblyName>$newProjectName</AssemblyName>" | sc $projectFile
	# other project references
	gci -Recurse -Include *.csproj |% { (gc $_) -replace "..\\$projectName\\$projectName.csproj", "..\$newProjectName\$newProjectName.csproj" | sc $_ }
	gci -Recurse -Include *.csproj |% { (gc $_) -replace "<Name>$projectName</Name>", "<Name>$newProjectName</Name>" | sc $_ }
	# solution 
	gci -Recurse -Include *.sln |% { (gc $_) -replace "\""$projectName\""", """$newProjectName""" | sc $_ }
	gci -Recurse -Include *.sln |% { (gc $_) -replace "\""$projectName\\$projectName.csproj\""", """$newProjectName\$newProjectName.csproj""" | sc $_ }


I'm not totally enamoured with the find/replace bits, but they seem to work.

Any improvements will of course be welcomed :)