Changing the SharePoint Game with PowerShell Scripts
As administrators of SharePoint 2010, the introduction of PowerShell was really a game changer for repeatable processes that need to do done in order to make the system healthy and useful. In SharePoint 2007, we could utilize PowerShell, but all the time we needed to load the DLL using the System. Reflection namespace like this:
With the introduction of the SharePoint Management Shell in SharePoint 2010, however things get a little more interesting when wanting to run a scheduled task as the way that the PowerShell snap-in is loaded for this management shell. In order to setup a scheduled task to run a script that utilizes some of the SharePoint cmdlets, you need to call the main powershell.exe that resides in the system32 folder, but then also load the configuration file that the management shell loads as well as call your script. The following steps are needed to run a scheduled task to call a script that utilizes some SharePoint cmdlets:
Open up the Task Scheduler and select Create Task from the actions pane:
From the new Create Task pane, of course put a scheduled task name but make sure that Run whether user is logged on or not is selected as well as the Run with highest privileges (think of it as Run as Administrator)
Make sure that you setup a schedule on the triggers pane, but because nothing is really new there we will skip over to the Actions pane where the magic happens. When you add a new action to be called in the program / script section you need to specify the location of the main powershell.exe file which, by default, resides at C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe. In the Add arguments section there are two specific arguments that need to be added:
- psconsolefile: The PowerShell snap-in that adds the SharePoint cmdlets. This should be set to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1
- command: This is the path for where your script is located.
All in all this is a sample of what one of our scripts looks like to manage user profile images on our Intranet:
-psconsolefile “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1″ -command “E:\PowerShell\Set-SPProfilePictureUrl.ps1″
Of course, to run any script you need to make sure that the Execution-Policy is set to at a minimum RemoteSigned.