Recently i have been doing pair programming with my colleague Andre and thanks to him, i learned powershell programming. Most of the code files in the project need some kind of copyright information at the top. One way to add these headers is by using code formatting tools like stylecop or resharper. But with my newly acquired skills i believe its better to write a small script to append the header on all code files using powershell.
Problem: (Add copyright information to all the files with a given extension found in the target and its subdirectories.)
- Read all the files from target directory and its subdirectories with given extension.
- Append header text at the beginning of the file with copyright information.
Solution:
param($target = "c:\\tmp", $companyname = "Lab49")$header = "//-----------------------------------------------------------------------// <copyright file=""{0}"" company=""{1}"">// Copyright (c) {1}. All rights reserved.// </copyright>//-----------------------------------------------------------------------`r`n"function Write-Header ($file) {$content = Get-Content $file$filename = Split-Path -Leaf $file$fileheader = $header -f $filename,$companynameSet-Content $file $fileheaderAdd-Content $file $content}Get-ChildItem $target -Recurse | ? { $_.Extension -like ".txt" } | % {Write-Header $_.PSPath.Split(":",3)[2]}
March 13, 2010 at 8:03 pm
Neat. Definitely a good compliment to the VS snippet that I’ve got on my tools project:
http://github.com/indexzero/dotnet-tools
August 30, 2012 at 9:43 pm
Hi Kishor,
You really helped me!
I posted a slightly modified version of your script here:
http://www.leniel.net/2012/08/inserting-copyright-notice-banner-in-all-source-code-files-with-powershell.html
Keep sharing…
Best,
Leniel
February 22, 2013 at 12:29 am
Hey there! This post could not be written any better!
Reading this post reminds me of my good old room mate!
He always kept talking about this. I will forward this article to him.
Pretty sure he will have a good read. Thank
you for sharing!