Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet

Wednesday, April 15th, 2009

While doing SharePoint development and deployment, you are constantly on the lookout for the Public Key Token of the assembly you will be deploying to SharePoint as it has to be set as a Safe Assembly, and in order to do this you need to know the Public Key Token. To automate this process better, and in my continual quest to move my entire development process to PowerShell, I decided it was time to write a cmdlet to get me the Public Key token of an assembly, as the SN.exe tool provided by Microsoft doesn't give you an easy option of getting just the Public Key token (it has a bunch of literals around it).

So without further ado, here is the code for the cmdlet:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.IO;
using System.Runtime;
using System.Reflection;