Monday, January 28, 2013

Load SharePoint PowerShell Commandlets in the PowerShell ISE

We know that Windows PowerShell ISE is a nice editor for writing and executing PowerShell scripts. But for SharePoint Administrators/Developers none of the SharePoint PowerShell Command-lets are loaded by default, so need to Add the Snap-In explicitly. But I wanted to load the SharePoint snap-in to ISE one-time and use it further without loading again-and-again.

Execute the below PowerShell script in ISE to load the snap-in to ISE profile.
if (!(test-path $profile.AllUsersAllHosts)) 
   new-item -type file -path $profile.AllUsersAllHosts-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
Now close and open the ISE, It will load SharePoint snap-in automatically. Intellisense is available for all the SharePoint Command-lets.

Alternatively a single line of statement can be added to ISE to load the SharePoint SnapIn
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
Hope this saves your time... 

Enable PowerShell ISE Editor

Good news for all the PowerShell developers/users. I know writing or editing a PowerShell script in Notepad or Visual Studio is not at all user-friendly and annoying. To overcome all such problems we can enable PowerShell ISE which provides a nice interface with script and output windows side by side.

Windows Server 2008 R2:
Execute the below commands on PowerShell command prompt.
1. Import Module Server Manager
    Import-Module ServerManager

2. Now search for PowerShell ISE feature in Windows Features
   Get-WindowsFeature -Name *PowerShell*

3. Verify PowerShell-ISE feature exists in previous result and add the feature
   Add-WindowsFeature PowerShell-ISE

Now the PowerShell ISE enabled on the server. To open the editor check for the Windows PowerShell ISE shortcut in start menu or open the PoserShell command prompt and execute the command ise.

Windows Server 2012:
PowerShell ISE comes by default.

Hope this helps making PowerShell scripts creation and execution easy and simple.