Add SharePoint PowerShell cmdlets to your default PowerShell Profile

One of the most annoying things to me in dealing with all of the great features in SharePoint 2010 and the administration of it through PowerShell is that I have to go through the menu system to get to it.

I have a perfectly functional QuickLaunch Powershell icon right next to my Windows Start icon in the bottom left corner of my Windows 2008 Server R2 install.  However, it doesn’t have the SharePoint cmdlets installed.  I also have the SharePoint cmdlets loaded into a SharePoint Powershell through Start-> All Programs –> Microsoft SharePoint 2010 Products –> Microsoft SharePoint 2010 Management Shell.  However, this is downright inconvenient.

One of the problems here is versioning.  The SharePoint Powershell cmdlets are written as a v1.0 snapin.  However, 2008 Server R2 has PowerShell 2.0 natively installed.  Also, upgrades to other OS’s specifically to PowerShell as well as other potential automatic updates are not always going to be v1.0 only.  PowerShell 2.0 brings in a number of desirable advancements, two of which being module packaging and remote execution.  For these reasons, I don’t want to live in the PowerShell 1.0 world any longer than I have to.  But I use PowerShell for SharePoint 2010 more than for anything else.

I have here a dilemma.   This dilemma is solved by customizing my PowerShell profile.  Just like in the old BSD Unix days where you “dot in your dot profile” settings, PowerShell has the ability to execute a profile settings file when you start up either a command shell or the ISE. 

Probably the easiest way to fix this dilemma is found in some code published by TomWis on MSDN blogs here:

http://blogs.msdn.com/b/tomwis/archive/2010/01/05/enable-sharepoint-powershell-commandlets-in-the-powershell-ise.aspx

With a few quick lines of code you can modify and save your profile settings to include the SharePoint Powershell snapin. 

if (!(test-path $profile )) 
{ 
    new-item -type file -path $profile -force 
} 
 
 
$cmd = 'if((Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) 
{ 
    Add-PSSnapIn "Microsoft.SharePoint.Powershell" 
}'
 
out-file -FilePath $profile -InputObject $cmd -Append

You can cut and paste that code into a PowerShell started up with your little icon next to the Windows icon in QuickLaunch.  That will forever modify your profile so that the SharePoint cmdlets are loaded when you start a shell via that icon.

Well what about all the really cool scripts I write with the PowerShell ISE?  you ask.  The same goes for startup profiles for the ISE.  Cut and paste the above code into your ISE Shell (started by right-clicking the QuickLaunch icon previously mentioned and selecting the “Windows PowerShell ISE” menu option).

What this code does is saves the big if statement with the Get-PSSnapin to a .ps1 file attached to your profile.  You can see the location of this file by typing in $profile and hitting <Enter> in your PowerShell shell.  These files are stored in your Documents directory, under WindowsPowerShell, and the files are named Microsoft.PowerShell_profile and Microsoft.PowerShellISE_profile accordingly.  The if loop at the start of the code tests to see if you have a directory already set up to hold profile settings and if there is not one, it creates that directory.

So now all of the features wrapped into the “Microsoft.SharePoint.PowerShell” SnapIn including all the cmdlets for managing your SharePoint 2010 sites are right there at your fingertips.

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.