Datasheet
10 CHAPTER 1
•
WINDOWS POWERSHELL 101
you will now have the bene t of integrated debugging capabilities, syntax highlight-
ing, and of course, a simple editor in which you can construct your routines without
having to execute them line by line.
An alternative to manually executing the
Add-PsSnapin
cmdlet would be
to update your default user pro le. is will enable you to use the SharePoint
PowerShell cmdlets in any editor for any user. To update your pro le, run the fol-
lowing from any Windows PowerShell console window or the Windows PowerShell
ISE to verify and create your pro le script:
if (!(Test-Path $profile.AllUsersAllHosts)) {
New-Item -Type file -Path $profile.AllUsersAllHosts -Force
}
Now that the pro le script exists, you can edit it. From the Windows PowerShell
ISE, run the following command:
Psedit $profile.AllUsersAllHosts
is will load your pro le script into the editor. If you are in a standard Windows
PowerShell window, type
ise $profile.AllUsersAllHosts
instead, and this
will launch the Windows PowerShell ISE with the script le open for editing. Now
add the following text to the script and save the le:
if ($host.UI.RawUI.WindowTitle -ne i
“Administrator: SharePoint 2010 Management Shell”) {
$ver = $host | select version
if ($ver.Version.Major -gt 1) {
$host.Runspace.ThreadOptions = “ReuseThread”
}
if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” i
-ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin “Microsoft.SharePoint.PowerShell”
}
}
is script does basically the same thing as the
sharepoint.ps1
script, but it adds
a further check to make sure that it is not run if the host is the SharePoint 2010
Management Shell. It also does a check to make sure the SharePoint PowerShell
snap-in isn’t already loaded. It’s not important that you understand every piece of
this script at this time because we’ll be covering the concepts demonstrated in this
script throughout this chapter and the next.
c01.indd 10c01.indd 10 5/16/2011 11:12:31 AM5/16/2011 11:12:31 AM