PowerShell ISE: Make ISE ready for SharePoint cmdlets

 

As SharePoint administrators and developers we do use Windows PowerShell cmdlets for creating SharePoint scripts.

PowerShell ISE is very powerful tool for scripting. However, we need to load the SharePoint snap-in everytime we load ISE to work with SharePoint cmdlets.

I know it’s not a big deal for most people, but for folks like me who do not want to type it everytime I open PowerShell ISE window, I would rather prefer it to be loaded automatically as it does for SharePoint Management Shell.

I know there are numerous blogs out there for the same requirement. Most of (all) the blogs I found involve doing some manual step by copying and pasting stuff in some file. I wouldn’t do that. Run a script and it should do it for me.

So, I am writing this blog.

This simple script would do the trick and you run it once and forget it.

 if (!(test-path $profile.AllUsersAllHosts)) 
{
    new-item -type file -path $profile.AllUsersAllHosts -force;
}
#Clear-Content $profile.AllUsersAllHosts;
Add-Content $profile.AllUsersAllHosts '$ver = $host | select version;
if ($ver.Version.Major -gt 1) 
{
    $host.Runspace.ThreadOptions = "ReuseThread";
}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell";
}';

A little (mind it !) explanation:

The script would open the profile file for All Users (so it would be for all users using PowerShell ISE on that machine) & All Hosts (PowerShell ISE or PowerShell command Prompt). If the profile file is not created, it would create one. Once created, it’ll push the content in that file.

N.B. It will append the text to the existing file. So if you run multiple times, you’ll have the same coded added multiple times. Wouldn’t make much difference except ISE would have to go through the code unnecessarily.

You can verify the contents using :

 psEdit $profile.AllUsersAllHosts;

When ISE loads it loads the profile file automatically and it loads the SharePoint snap-in for you.

That’s it.. You can now focus on your development more by a minute than loading the snap-in every time.

And yeah, if you want to do it for your account only, use $profile.CurrentUserAllHosts instead of $profile.AllUsersAllHosts in the script.