Delen via


Writing and running scripts with PowerShell ISE

Using SharePoint 2010 Management Shell is a very inefficient way to run PowerShell scripts. It is great for quickly running PowerShell commands that are a few lines long, but when is this a typical scenario these days with SharePoint, SQL or other technologies, not often. I always wanted a better way to execute scripts as opposed to saving as a .ps1 text file, launching PowerShell window, change directory to find the .ps1 file and then finally run my scripts, to me this is antiquated with way too may steps involved. I want to highlight my script or portions of my script with my mouse and click one button to run it, that's all. I got this idea from my short lived days as an SQL developer where you can highlight a piece of SQL and click run to return results. When I joined Microsoft, I went to a PowerShell session held by Josh Gavant and saw how he could select a piece of PowerShell Script and run it, well the sky turned blue again. I never looked back and I never load the SharePoint Management Shell, but I run hundreds of commands all the time. Read and see how you can improve your life.

PowerShell v2 ships with PowerShell ISE, that is much easier to use while working with scripts. This is available by default on Windows 7 and can be enabled as a feature on Windows Server 2008, using the following PowerShell command: Add``-WindowsFeaturePowerShell`` -ISE (a good example of when to use the Management Shell) You can now find PowerShell ISE shortcuts in the Start Menu > Accessories (do not use (x86) with SharePoint). If you are using Windows 8, click Window button, enter PowerShell to find the Tile, right click and add it to the task bar. From now on, right click on the PowerShell Icon and click PowerShell ISE to load our new tool, see graphic below.

Figure 1: Launching ISE from Windows 8

 

Figure 2: PowerShell ISE in action

 

However, when you start PowerShell ISE, you’ll find that it only works with Windows cmdlets. For PowerShell ISE to work with SharePoint cmdlets (or other technologies), you’ll need to add a PowerShell Snapin with a few other recommended settings. You will want these setting to load every time ISE loads so modify your PowerShell Profile to host the correct commands as follows. (for Steps 1 - 3, copy and paste the code into a new Tab in ISE and run it by clicking F5)

1. Create a new profile page for all users

     if (!(test-path $profile.AllUsersAllHosts)) 
     {new-item -type file -path $profile.AllUsersAllHosts-force}
  
2. Edit the new profile, this will load the newly created page in ISE
 
     psEdit $profile.AllUsersAllHosts

3.  In the open page, right in ISE, called profile1.ps1, add the following code to the page and click save 

     $ver = $host | select version
     if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
     Add-PsSnapin Microsoft.SharePoint.PowerShell
     Set-location $home
 
  
4.  Save profile.ps1 page and close PowerShell ISE, re-open ISE and try running SharePoint commands such as Get-Command *SP* to return list of SharePoint cmdlets.

 

I hope you enjoy your new found methodology to run PowerShell Scripts in SharePoint or most other Microsoft Technologies.

Comments

  • Anonymous
    January 01, 2003
    Thanks for that
  • Anonymous
    September 22, 2014
    omate