How to register custom powershell cmdlet

I have created a custom powershell cmdlet and here we are going to see how to register the snapin. Install the snapin:

Install.bat

@SET GACUTIL="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\gacutil.exe"

@SET INSTALLUTIL="C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe"

%GACUTIL% -if %~dp0anavijai.SP2010.PowerShell.dll

%INSTALLUTIL% %~dp0anavijai.SP2010.PowerShell.dll

Pause

Register the snapin:

Add-PSSnapin anavijai.SP2010.PowerShell

Get-Command – Module anavijai.SP2010.PowerShell

If you register the custom powershell cmdlet using the Add-PSSnapin command the snapin will be available only for that session. Each and every time you go for a new session you will need to register the snapin using Add-PSSnapin. You can register the custom powershell cmdlet in another way by creating an xml file and deploying the file in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration". So that you do not need to register the cmdlet each and every time.

Create an xml file:

anavijaiSP2010Cmdlet.xml

  1. <ps:Config xmlns:ps="urn:Microsoft.SharePoint.PowerShell"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="urn:Microsoft.SharePoint.PowerShell  SPCmdletSchema.xsd">

  4. <ps:Assembly Name="anavijai.SP2010.PowerShell, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bcaceceb1f6761bc">

  5.  <ps:Cmdlet>                            

  6. <ps:VerbName>Get-SPWebTitle</ps:VerbName>                            

  7. <ps:ClassName>anavijai.SP2010.PowerShell.GetSPWebTitle</ps:ClassName>                           

  8. <ps:HelpFile>anavijai.SP2010.PowerShell.dll-help.xml</ps:HelpFile                  

  9. </ps:Cmdlet>       

  10. </ps:Assembly>

  11. </ps:Config>


Deploy the file in the "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration".

You can include the same in the Installer.bat

xcopy %~dp0anavijaiSP2010Cmdlet.xml "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration"

Now we will be able to access the Get-SPWebTitle directly without using the Add-PSSnapin command.