![Page 1: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/1.jpg)
Introduction to PowerShell for SharePoint Developers and
Administrators
Michael BlumenthalPSC Group, LLC
![Page 2: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/2.jpg)
Who is Michael Blumenthal?
• Sr. Solution Architect at PSC Group• CSPUG Co-Leader• INETA Champ 2010-2013• 19 years in IT Consulting• 11 years working with SharePoint
(2003 - 2014)
• 7 years working with PowerShell• Twitter: @MichaelBL
![Page 3: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/3.jpg)
No Compiling
!
No Packagin
g!
Just Code & Go!
Why PowerShell?
![Page 4: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/4.jpg)
PowerShell puts the SharePoint Engine at your fingertips!
• It’s Easy to Get Started!1• Learn the PowerShell
Syntax2
• Real World Examples3
• More Resources4
• Q&A 5
![Page 6: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/6.jpg)
Getting Started with PowerShell
Windows Server 2003• Download
Windows Server 2008• Install
Server2008 R2 +• Run (Add ISE)• Upgrade to V4
![Page 12: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/12.jpg)
Learn to use PowerShell with SharePoint!
Symbols & Keywords
Using the SharePoint API
Creating and Running Scripts
![Page 13: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/13.jpg)
Symbols, Keywords, and Syntax! Oh My!
• Variables1
• Commands2
• Piping3
• Comparisons4
• Flow Control5
• Filtering6
![Page 14: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/14.jpg)
Punctuation Pronunciation
Symbol Called Symbol Called
$ Dollar sign, money _ Underscore
# Hash, Pound [ ] Square Brackets
| Pipe, vertical bar . Dot, point, period
{ } Curly braces < > Angle Brackets
“ Double Quote, tick - Dash, hyphen, minus
: Colon % Percent sign
( ) Parentheses ; Semi-colon
+ Plus = Equals, is
! Bang, not /, \ Slash, backslash
1$#|
![Page 15: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/15.jpg)
Variables begin with a $
• Case Insensitive, Dynamic typing
$foo
$true, $false, $profile, $null
$foo = “Hello, World”
1
![Page 17: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/17.jpg)
Commands are called cmdlets.
Verb-Noun
Built-in, ExtensibleGet-Help &
Help
Get-Member
2
![Page 21: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/21.jpg)
Making Comparisons4Operator Meaning Operator Meaning
-eq Equals -le Less Than or Equal To
-ne Not Equals -like Wildcard Match
-gt Greater Than
-notlike Not (Wildcard Match)
-ge Greater Than or Equal To
-match Reg. Exp. Match
-lt Less Than -notmatch
Not (Reg. Exp. Match)
![Page 23: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/23.jpg)
Taking Control of the Flow
5
• For (Init;Test;Repeat) {Commands}• for($i=1; $i -le 10; $i++) {Write-Host $i}For
• Foreach (Item in Collection) {Commands}• Foreach ($web in $site.AllWebs) {$web.Title}
ForEach
• If (Test) {Commands} • if ($web.Title –ne “”) {Write-Host $web.Title}
If
• While (Condition){Commands}• while($val -ne 3){$val++; Write-Host $val}While
![Page 25: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/25.jpg)
Where-Object6
•Where {<Test>}
Syntax
• V1&2:Dir | Where {$_.Name –like “B*”}
• V3:Dir | where Name –like B*
Example
![Page 26: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/26.jpg)
Using the SharePoint API
• Getting an SPSite1
• Manipulating It2
• Cleaning Up3
![Page 27: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/27.jpg)
Highlights from the SharePoint Object Model
SPField
SPListItem
SPList
SPWeb
SPWebApplication
SPFarm
SPSite
![Page 28: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/28.jpg)
Loading SharePoint Cmdlets
Even in MOSS 2007:[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Loading SharePoint DLLs
C:\...\14 or 15\CONFIG\POWERSHELL\Registration\
SharePoint.ps1
![Page 29: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/29.jpg)
A Word About Memory Management
SPWeb SPSite
Inline In Script
Dispose
![Page 32: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/32.jpg)
Real World Examples
• Check the Farm Version• Check Versioning on all document Libraries• Create List Items• Export Web App Properties to a file• Bulk Site Creation
![Page 33: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/33.jpg)
What’s your Farm Version?
PS C:\Users\Administrator> $(get-SPFarm).BuildVersion
Major Minor Build Revision----- ----- ----- --------14 0 6109 5002
![Page 36: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/36.jpg)
Check Doc Lib Versioning Settingsfunction global:show-all-doclibs ($web){$web.Lists | where-object {($_.Hidden -ne $true) -and ($_.BaseType -eq "DocumentLibrary")} }
function global:show-all-doclib-versettings ($web)
{show-all-doclibs ($web) |select-object -property Title, EnableVersioning, MajorVersionLimit, EnableMinorVersions,MajorWithMinorVersionsLimit,forceCheckout}
$site = get-spsite “http://server/path”
show-all-doclib-versettings $site.RootWeb
![Page 38: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/38.jpg)
Practical Uses• Bulk Create Sites1
• List Item CRUD2• Create data for test cases3
• Associate Workflows with a List4
• Work across site collections5
• Deployment Scripting6
• Identify files that won’t upload7
![Page 39: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/39.jpg)
More Practical Uses
• Sync Wep App Properties8
• Install SharePoint9
• Repeatably Manage Content10
• Update Field Definitions11
• Edit MP3 Metadata, Make Flashcards12
![Page 41: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/41.jpg)
Audio Alerts
• Stick this at the end of your long running script:
$Voice = new-object -com SAPI.SpVoice $Voice.Speak(“Deployment is done!")
![Page 43: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/43.jpg)
Bulk Site Creation
Site Definitions in V. Studio• Not an answer by
themselves• Define site content• Intended for reuse
Mismatch to one time need• CAML and PITA• Harder: Making it data
driven• Change Site Def ->
Recreate Site
PowerShell & Excel & UI
• Well suited for one time “blow in’s”
• Define the site template in the UI or use standard
• Save as a template Even pub sites - sometimes
• PowerShell has easy loops• Data driven from a CSV• Changes -> Mod Scripts
![Page 44: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/44.jpg)
The PowerShell Solution
• Read the list of sites from CSV• Loop:
Create Site Configure Site
Turn on Features Set Master Pages, Welcome Page Hide Libraries, set versioning Adjust Navigation
Add Lists, Libraries, Pages, Web parts, etc
• Loop again & again if needed – iterative!
![Page 46: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/46.jpg)
![Page 52: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/52.jpg)
Resources Summary
PowerShell Product Team Blog
TechNet Script Center PowerShell in Action b
y Bruce Payette CodePlex
: PSBBs (mine) CodePlex:SPInstaller Blog.BlumenthalIT.Net SharePointJack.com
• Jeff Hicks• Gary LaPointe • Raymond Mitchell• Todd Klindt• POSHCODE.ORG• get-spscripts.com• SPYam
![Page 53: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/53.jpg)
Script something today!
It’s Easy to Get Started!
Learn & Use the PowerShell
Syntax
More Resources
In Review…
![Page 54: SP24S053 Introduction to PowerShell for SharePoint Developers and Administrators](https://reader035.vdocuments.us/reader035/viewer/2022081400/5560b752d8b42a033c8b4b7b/html5/thumbnails/54.jpg)
Questions
• Michael BlumenthalSharePoint ArchitectPSC Group, LLC
• [email protected]• psclistens.com• www.cspug.org• Twitter: @MichaelBL• SPYam
Thank you for your time today.