I have a better version of this PowerShell script if you’re interested.
I suggest you read this post first (there’s a bonus point) then go take a look at the new one here:
Updated PowerShell script to insert copyright notice/banner/header in all source code files
Today my client asked for the source code of a project we worked together. He wants to submit it to patent review and he wanted to include a copyright notice in each C# code file (.cs).
I googled about a way of accomplishing this and found this post:
Use a Visual Studio Macro to Insert Copyright Headers into Source Files
It looked promising but as I’m working with Visual Studio 2012 I just hit a road block because it dropped the support for Macros. So I had to find some other way… in the meantime I asked a question at StackOverflow to see if someone had any good advice regarding this. Right now there’s one nice answer but it requires manual work. I’m really interested in an automated process (do the work in a batch fashion) because there are lots of .cs files in the Visual Studio solution comprised of 7 projects.
Some more Googling with the right words (read PowerShell) and I was able to find something that could do the work. Kishor Aher wrote about it Powershell – Copyright header generator script. Oh God, the internet is really amazing thingy! Remember to just search for the right words…
Now it was just a matter of adapting the PowerShell script to my needs. Here is it:
param($target = "C:\MyProject", $companyname = "My Company", $date = (Get-Date))
$header = "//-----------------------------------------------------------------------
// <copyright file=""{0}"" company=""{1}"">
// Copyright (c) {1}. All rights reserved.
// <author>Leniel Macaferi</author>
// <date>{2}</date>
// </copyright>
//-----------------------------------------------------------------------`r`n"
function Write-Header ($file)
{
$content = Get-Content $file
$filename = Split-Path -Leaf $file
$fileheader = $header -f $filename,$companyname,$date
Set-Content $file $fileheader
Add-Content $file $content
}
Get-ChildItem $target -Recurse | ? { $_.Extension -like ".cs" } | % `
{
Write-Header $_.PSPath.Split(":", 3)[2]
}
As you see above, the script will place the copyright notice in every .cs file that lies within the folder C:\MyProject. So let’s say you have a Visual Studio solution with various projects – the script will traverse all files placing the notice in every .cs file it finds along the way…
I named the script Copyright.ps1.
To make this work I just opened PowerShell command line, navigated to the folder where I placed the script file and typed .\Copyright.ps1 to run the script:
Figure 1 - Executing Copyright PowerShell script using PowerShell command line tool
You’re done!
Enjoy.
Notes
- Close Visual Studio solution before running the PowerShell script. Otherwise you may get some error message telling you that some file is in use.
- Be sure to have your source code files commited in a source control repository before running the scripts so that if anything goes wrong you can just revert to the previous state.
- Beware that if you run the script twice it'll add the copyright notice twice in each file.
A big thank you to Kishor for providing us the PowerShell script. I’m not that experienced with PowerShell but adapting existing scripts is something easy to do IMHO.
Bonus
To get support for PowerShell scripting in Visual Studio with syntax highlighting and IntelliSense, there’s an awesome and free product out there. It’s called PowerGUI. In order to use it, download PowerGUI here and then install its accompanying Visual Studio extension available here. As I’m using Visual Studio 2012 I had to install the alpha release available here.
See for yourself the awesomeness:
Figure 2 - PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting