June 21st, 2015

Weekend Scripter: Start PowerShell Scripting for Office 365 Sites

Doctor Scripto
Scripter

Summary: Guest blogger, Michael Blumenthal shows how to findWindows PowerShell cmdlets that are available for Office 365 sites.

Microsoft Scripting Guy, Ed Wilson, is here. Today we have a new guest blogger, Michael Blumenthal, who we met in Chicago in May at Microsoft Ignite. Here is a little bit about Michael:

Photo of Michael Blumenthal

Michael Blumenthal is a technical solution evangelist with PSC Group and a Microsoft Office 365 MVP. As such, he is an experienced architect for SharePoint, and business technology advisor. Michael has deep SharePoint expertise, with two decades of development, infrastructure, and administration experience. He’s designed, implemented, and supported intranets, extranets, and Internet sites for clients in markets as diverse as banking, energy, government, and transportation.

Michael holds numerous Microsoft certifications (including most of the SharePoint certifications) and the Certified Associate in Project Management (CAPM) from the Project Management Institute. He can be found year round speaking on technology topics, including SharePoint and Windows PowerShell, at Chicago-area technology events and user groups.

Michael's contact info:

Blog: Michael Blumenthal, Office 365 MVP
Twitter: @michaelbl

You need Windows PowerShell 4.0

I assume you already have Windows PowerShell 4.0 installed. You will want two cmdlet libraries:

First, get the SharePoint Online Management Shell and install it. This will give you…wait for it…

37 SharePoint Online cmdlets.

I counted them this way:

get-command -Noun SPO* | where moduleName -like Microsoft.Online.SharePoint* | measure

Thirty-seven? Yes, only 37! But in SharePoint 2013 on premise, I had hundreds of SharePoint cmdlets available to me. What on earth will I do?

First, let’s look at what these 37 are:

PS C:\Windows\system32> get-command -Noun SPO* | where moduleName -like Microsoft.Online.SharePoint* |
Select-Object Name | Sort-Object Name

Name

—-

Add-SPOUser

Connect-SPOService

ConvertTo-SPOMigrationTargetedPackage

Disconnect-SPOService

Get-SPOAppErrors

Get-SPOAppInfo

Get-SPODeletedSite

Get-SPOExternalUser

Get-SPOSite

Get-SPOSiteGroup

Get-SPOTenant

Get-SPOTenantLogEntry

Get-SPOTenantLogLastAvailableTimeInUtc

Get-SPOTenantSyncClientRestriction

Get-SPOUser

Get-SPOWebTemplate

New-SPOMigrationPackage

New-SPOSite

New-SPOSiteGroup

Remove-SPODeletedSite

Remove-SPOExternalUser

Remove-SPOSite

Remove-SPOSiteGroup

Remove-SPOTenantSyncClientRestriction

Remove-SPOUser

Repair-SPOSite

Request-SPOPersonalSite

Request-SPOUpgradeEvaluationSite

Restore-SPODeletedSite

Set-SPOSite

Set-SPOSiteGroup

Set-SPOTenant

Set-SPOTenantSyncClientRestriction

Set-SPOUser

Submit-SPOMigration

Test-SPOSite

Upgrade-SPOSite

What about missing cmdlets?

What? No SPOWeb? Well, let’s get more cmdlets then! Gary LaPointe to the rescue! Go to APTILLON Sharepoint Automation and click Installer: Lapointe.SharePointOnline.PowerShell.msi.

Run that.

Now how many cmdlets do you have?

get-command -Noun SPO* | where moduleName -like Lapointe.SharePointOnline.* | measure

45! Yay! What are they?

PS C:\Windows\system32> get-command -Noun SPO* | where moduleName -like Lapointe.SharePointOnline.* |
Select-Object Name | Sort-Object Name

Name

—-

Add-SPOContentType

Add-SPOField

Add-SPOListViewWebPart

Add-SPOSolution

Add-SPOWebPart

Connect-SPOSite

Disable-SPOFeature

Disconnect-SPOSite

Enable-SPOFeature

Export-SPOSearchConfiguration

Export-SPOTaxonomy

Get-SPOContentType

Get-SPOContextSite

Get-SPOEventReceiver

Get-SPOFeature

Get-SPOFile

Get-SPOFolder

Get-SPOList

Get-SPOTerm

Get-SPOTermGroup

Get-SPOTermSet

Get-SPOTermStore

Get-SPOWeb

Get-SPOWebPart

Import-SPOSearchConfiguration

Import-SPOTaxonomy

Install-SPOSolution

Invoke-SPORestMethod

New-SPOContentType

New-SPOField

New-SPOFile

New-SPOList

New-SPOListFolder

New-SPOListItem

New-SPOTermGroup

New-SPOTermSet

New-SPOWeb

New-SPOWikiPage

Remove-SPOContentType

Remove-SPOList

Remove-SPOWeb

Set-SPOList

Set-SPOWeb

Set-SPOWebTheme

Set-SPOWikiPageLayout

Even more SharePoint cmdlets

Ah, that’s more like it! Cmdlets to work with lists and libraries. Now we’re talking!

However, if you want more cmdlets and some examples, take a look at this CodePlex project: Client-side SharePoint PowerShell.

Now that we have the cmdlets in the SharePoint Online Management Console and the cmdlets from Gary LaPointe, we can do things like this:

function global:Get-SPOWebInfo {

<#

.SYNOPSIS

         This demonstrates the use of some cmdlets for Office 365 Sites.

    .DESCRIPTION

         This demonstrates the use of some cmdlets for Office 365 Sites using both Microsoft's and Gary LaPointe's cmdlets for SharePoint Online.

    .PARAMETER TenantAdminURL

        The Admin URL for your O365 Tenant.  For example, if your tenant is https://foo.sharepoint.com, your Admin URL is https://foo-admin.sharepoint.com.

    .PARAMETER  Login

        Your O365 login email.

    .PARAMETER  Password

        Your O365 login password.

    .EXAMPLE

     get-SPOWebInfo -TenantAdminURL foo.sharepoint.com -Login login@somedomain.com -password SomeStrongPasswordGoesHere 

     .OUTPUTS

         Writes out the URL of every web in every site.

    .NOTES

         Author: Michael Blumenthal, PSC Group LLC

#>

Param

          (

            [parameter(Mandatory=$true, HelpMessage="Enter the Admin URL for your O365 tenant.")]

            [Uri]

            $TenantURL,

             [parameter(Mandatory=$true, HelpMessage="Enter your admin login.")]

            [System.String]

            $Login,

            [System.Security.SecureString][Parameter(Mandatory=$true, HelpMessage="Enter your super secret strong password.")]

            $password

          ) 

    $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Login, $password

    Connect-SPOService -Url $TenantURL -Credential $credential 

     $SiteCollections = Get-SPOSite -Detailed

     foreach ($SiteCollection in $SiteCollections)

     {

      write-host $SiteCollection.Url -foregroundcolor Blue 

            $rootwebUrl = $SiteCollection.Url

            try {

                Connect-SPOSite -Credential $credential -Url $rootwebURL 

                $allwebs = Get-SPOWeb -Detail 

                #each web is a Microsoft.SharePoint.Client.Web

          foreach ($web in $allwebs)

          { 

                    write-host "In " $web.Title "at" $web.Url           

                }

            }

            Catch [Microsoft.SharePoint.Client.ServerUnauthorizedAccessException] {

                Write-Host -ForegroundColor Yellow "SUAE error."

            }

            Catch [System.Net.WebException] {

                Write-Host -ForegroundColor Yellow "WebException."

                Write-Host -ForegroundColor Yellow $_.exception.Message

            }

            Catch {

                $ErrorMessage = $_.Exception.Message

                $FailedItem = $_.Exception.ItemName

                $ExceptionType = $_.Exception.GetType().FullName;

                Write-host -ForegroundColor Yellow "Other exception: " $ExceptionType

                Write-host -ForegroundColor Yellow Message: $ErrorMessage

                Write-host -ForegroundColor Yellow Failed Item $FailedItem

            }

     }

    Disconnect-SPOService 

}

get-command Get-SPOWebInfo 

 Let's check it out

Now run Get-SPOWebInfo, and give it a try!

Image of command output

~Michael

Thank you, Michael. Great introduction to finding and using SharePoint cmdlet resources.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Michael Blumenthal

    Looking at this several years later, the 3rd party cmdlet libraries listed above are no longer needed. Use the PowerShell cmdlets from the Patterns and Practices repository on GitHub: https://github.com/SharePoint/PnP-PowerShell