Powershell – Copyright header generator script

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.


param($target = "c:\\tmp", $companyname = "Lab49")
$header = "//-----------------------------------------------------------------------
// <copyright file=""{0}"" company=""{1}"">
//     Copyright (c) {1}. All rights reserved.
// </copyright>
function Write-Header ($file) {
$content = Get-Content $file
$filename = Split-Path -Leaf $file
$fileheader = $header -f $filename,$companyname
Set-Content $file $fileheader
Add-Content $file $content
Get-ChildItem $target -Recurse | ? { $_.Extension -like ".txt" } | % {
Write-Header $_.PSPath.Split(":",3)[2]

About these ads

7 Responses to “Powershell – Copyright header generator script”

  1. Charlie Robbins Says:

    Neat. Definitely a good compliment to the VS snippet that I’ve got on my tools project:


  2. Leniel Macaferi (@johnleniel) Says:

    Hi Kishor,

    You really helped me! :)

    I posted a slightly modified version of your script here:


    Keep sharing…



  3. Monty Says:

    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!

  4. Find Me Landlords Insurance Says:

    Hello There. I discovered your weblog using msn.
    That is a very smartly written article. I’ll be sure to bookmark it and return to read more of your helpful info. Thanks for the post. I’ll certainly return.

  5. Meine Seite Says:

    Do you mind if I quote a couple of your articles as long as I
    provide credit and sources back to your webpage?

    My blog is in the very same area of interest as yours and my
    visitors would truly benefit from a lot of the information you present here.
    Please let me know if this alright with you.

  6. manju Says:

    thank you!

  7. | Inserting copyright notice/banner/header in all source code files with PowerShell script Says:

    […] 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 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: