Load SharePoint Snapin to Powershell ISE by Default

Problem:  If you use the PowerShell ISE to edit your SharePoint-PowerShell scripts, you may noticed that SharePoint cmdlets are not available by default until you explicitly load them with "Add-PSSnapin" cmdlet. This is because the ISE doesn't load SharePoint PowerShell Snap-ins by default.

Solution: Here is the nifty trick to load SharePoint snap-in in PowerShell ISE by default! All you have to do is: Add the SharePoint snap-in to your PowerShell ISE profile file, which is getting executed each time you fire PowerShell ISE! Here is how:
  1. To make sure that the profile file is created, run:
    #Check if the profile file exists already
    if (test-path $profile) 
        write-host "profile file already exists at: $profile" 
        #Create the profile file
        New-Item -type file -path $profile -force 
        write-host "profile file has been created!" 
    This script will create a new profile file, if it isn't created already! Your output will be something like:
    add sharepoint snapin to powershell ise
  2. Open the profile file you received from above script, Add the below line to it. (in my case, its: C:\Users\salaudeen\Documents\WindowsPowerShell\Micro
    Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
  3. Save and close
So, from now on, you don't need to explicitly load SharePoint PowerShell snapins as your first line of script! This will definitely save your time and enhance your scripting experience. To make this change for all users, use: $profile.AllUsersAllHosts instead of $profile in the above script.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.