Visual Studio Code PowerShell Extension: Create a Script Module for tasks in SharePoint Online
https://msdnshared.blob.core.windows.net/media/2016/08/0841.NinjaAwardTinySilver.pngSilver Award Winner
Summary
In this TechNet Wiki article let us walk you through a short demo to create a PowerShell function to do few tasks in SharePoint Online. We were working along with content managers and received few requirements like managing contents like Lists, User Profile reports etc. In short, they need request batching. Just one step ahead and created a module which allows them to do the task they need and can be done on the fly. This Article is to demo about Windows PowerShell Script module and just topping up with essence of SharePoint to show an example. You can download this sample module from here and refer Usage Screen shots.
References
Get-SPOList
This cmdlet is to Get SharePoint Online List and Libraries from the given URL.
Code
function`` `` Get-SPOList
{
``<#
.Synopsis
This cmdlet retrieves all the List and Libraries from the given URL
.DESCRIPTION
This cmdlet is built using Client Side Object Model.
.EXAMPLE
Get-SPOList -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com
Retrieves all the lists and libraries
.EXAMPLE
Get-SPOList -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com | ? {$_.Name -like '*List*'}
Retrieves all the lists and libraries which contains list in the title
.EXAMPLE
Get-SPOList -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com | ? {$_.Hidden -eq $true}
Retrieves all the lists and libraries which are hidden
.EXAMPLE
Get-SPOList -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com | ? {$_.BaseType -eq 'Document Library'}
Retrieves only document libraries
#>
[``cmdletbinding``(ConfirmImpact=``'Low'``,
HelpUri=``'https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.list_properties.aspx'``)]
``Param``(
[``Parameter``(Mandatory=``$true``)]
[``uri``]``$SPOUrl``,
[``Parameter``(Mandatory=``$true``)]
[``System.Management.Automation.CredentialAttribute``()]``$SPOCredential
)
``$ClientContext`` = [``Microsoft.SharePoint.Client.ClientContext``]::new(``$SPOUrl``)
``$ClientContext``.Credentials = [``Microsoft.SharePoint.Client.SharePointOnlineCredentials``]::new(``$SPOCredential``.UserName,``$SPOCredential``.Password)
``$Lists`` = `` $ClientContext``.Web.Lists
``$ClientContext``.Load(``$Lists``)
``$ClientContext``.ExecuteQuery()
``$ClientContext``.Dispose()
``$DefaultProperties`` = @(``"Name"`` , ``"Hidden"`` , ``"Created"`` , ``"ItemCount"``)
``$DefaultDisplayPropertySet`` = New-Object ``System.Management.Automation.PSPropertySet``(``"DefaultDisplayPropertySet"``,[``string``[]]``$DefaultProperties``)
``$PSStandardMembers`` = [``System.Management.Automation.PSMemberInfo``[]]@(``$DefaultDisplayPropertySet``)
``foreach``(``$List`` ``in`` ``$Lists``)
{
``$Results`` = [``PSCustomObject``]@{
Name = ``$List``.Title
Hidden = ``$List``.Hidden
Created = ``$List``.Created
ItemCount = ``$List``.ItemCount
NoCrawl = ``$List``.NoCrawl
LastItemModifiedDate = ``$List``.LastItemModifiedDate
BaseTemplate = ``$List``.BaseTemplate
BaseType = ``$List``.BaseType
AllowContentTypes = ``$List``.AllowContentTypes
}
``$Results`` | Add-Member `` -MemberType`` ``MemberSet`` `` PSStandardMembers`` ``$PSStandardMembers
``$Results
}
Get-SPOSiteCollection
This cmdlet is to retrieve all the site collections.
Code
function`` `` Get-SPOSiteCollection
{
``<#
.Synopsis
This cmdlet is to retrieve all the site collections.
.DESCRIPTION
This cmdlet is built using Client Side Object Model.
.EXAMPLE
Get-SPOSiteCollection -SPOUrl "https://contoso-admin.sharepoint.com" -spocredential SPOAdmin@contoso.onmicrosoft.com
.EXAMPLE
Get-SPOSiteCollection -SPOUrl https://chensoffice365-admin.sharepoint.com -SPOCredential Chendrayan@chensoffice365.onmicrosoft.com | ? {$_.Template -eq 'BLOG#0'}
.EXAMPLE
Get-SPOSiteCollection -SPOUrl https://chensoffice365-admin.sharepoint.com -SPOCredential Chendrayan@chensoffice365.onmicrosoft.com | ? {$_.Template -ne 'BLOG#0'}
.EXAMPLE
Get-SPOSiteCollection -SPOUrl https://chensoffice365-admin.sharepoint.com -SPOCredential Chendrayan@chensoffice365.onmicrosoft.com | ? {$_.WebsCount -gt 2}
#>
``Param``(
[``Parameter``(Mandatory=``$true``)]
[``uri``]``$SPOUrl``,
[``Parameter``(Mandatory=``$true``)]
[``System.Management.Automation.CredentialAttribute``()]``$SPOCredential
)
``$ClientContext`` = [``Microsoft.SharePoint.Client.ClientContext``]::new(``$SPOUrl``)
``$ClientContext``.Credentials = [``Microsoft.SharePoint.Client.SharePointOnlineCredentials``]::new(``$SPOCredential``.UserName,``$SPOCredential``.Password)
``$SPOTenant`` = [``Microsoft.Online.SharePoint.TenantAdministration.Tenant``]::new(``$ClientContext``)
``$SPOSiteCollections`` = `` $SPOTenant``.GetSiteProperties(``0``,``$true``)
``$ClientContext``.Load(``$SPOSiteCollections``)
``$ClientContext``.ExecuteQuery()
``$ClientContext``.Dispose()
``$DefaultProperties`` = @(``"SiteCollectionUrl"`` , ``"Template"`` , ``"WebsCount"``)
``$DefaultDisplayPropertySet`` = New-Object ``System.Management.Automation.PSPropertySet``(``"DefaultDisplayPropertySet"``,[``string``[]]``$DefaultProperties``)
``$PSStandardMembers`` = [``System.Management.Automation.PSMemberInfo``[]]@(``$DefaultDisplayPropertySet``)
``foreach``(``$SPOSite`` ``in`` ``$SPOSiteCollections``)
{
``$Results`` = [``PSCustomObject``]@{
SiteCollectionUrl = ``$SPOSite``.Url
Template = ``$SPOSite``.Template
WebsCount = ``$SPOSite``.WebsCount
Owner = ``$SPOSite``.Owner
StorageMaximumLevel = ``$SPOSite``.StorageMaximumLevel
StorageQuotaTye = ``$SPOSite``.StorageQuotaType
StorageUsage = ``$SPOSite``.StorageUsage
StorageWarningLevel = ``$SPOSite``.StorageWarningLevel
LockIssue = ``$SPOSite``.LockIssue
LockStatus = ``$SPOSite``.LockStatus
}
``$Results`` | Add-Member `` -MemberType`` ``MemberSet`` `` PSStandardMembers`` ``$PSStandardMembers
``$Results
}
Get-SPOUserProfile
This cmdlet is to retrieve user profile information
Code
function`` `` Get-SPOUserProfile
{
``<#
.Synopsis
This cmdlet is to create a New List or Library in the given site collection
.DESCRIPTION
This cmdlet is built using Client Side Object Model.
.EXAMPLE
Get-SPOUserProfile -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com
Displays AccountName , FirstName and SipAddress
.EXAMPLE
Get-SPOUserProfile -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com | Select *
Select All Properties! ``
.EXAMPLE
Get-SPOUserProfile -SPOUrl https://contoso.sharepoint.com -SPOCredential SPOAdmin@contoso.onmicrosoft.com | Select-Object Email , Interests
Allows to select the available property!
#>
``Param``(
[``Parameter``(Mandatory=``$true``)]
[``uri``]``$SPOUrl``,
[``Parameter``(Mandatory=``$true``)]
[``System.Management.Automation.CredentialAttribute``()]``$SPOCredential
)
``$ClientContext`` = [``Microsoft.SharePoint.Client.ClientContext``]::new(``$SPOUrl``)
``$ClientContext``.Credentials = [``Microsoft.SharePoint.Client.SharePointOnlineCredentials``]::new(``$SPOCredential``.UserName,``$SPOCredential``.Password)
``$SPOUsers`` = `` $ClientContext``.Web.SiteUsers
``$ClientContext``.Load(``$SPOUsers``)
``$ClientContext``.ExecuteQuery()
``$People`` = [``Microsoft.SharePoint.Client.UserProfiles.PeopleManager``]::new(``$ClientContext``)
``$DefaultProperties`` = @(``"FirstName"`` , ``"LastName"`` , ``"Email"``)
``$DefaultDisplayPropertySet`` = New-Object ``System.Management.Automation.PSPropertySet``(``"DefaultDisplayPropertySet"``,[``string``[]]``$DefaultProperties``)
``$PSStandardMembers`` = [``System.Management.Automation.PSMemberInfo``[]]@(``$DefaultDisplayPropertySet``)
``foreach``(``$SPOUser`` ``in`` ``$SPOUsers``)
{
``$SPOUserProfile`` = `` $People``.GetPropertiesFor(``$SPOUser``.LoginName)
``$ClientContext``.Load(``$SPOUserProfile``)
``$ClientContext``.ExecuteQuery()
``if``(``$SPOUserProfile``.Email -ne ``$null``)
{
``$SPOUserProfileProperties`` = ``$SPOUserProfile``.UserProfileProperties
``foreach``(``$SPOUserProfileProperty`` ``in`` ``$SPOUserProfileProperties``)
{
``$Results`` = [``PSCustomObject``]@{
AccountName = ``$SPOUserProfileProperty``.AccountName
FirstName = ``$SPOUserProfileProperty``.FirstName
LastName = ``$SPOUserProfileProperty``.LastName
DisplayName = ``$SPOUserProfileProperty``.PreferredName
Department = ``$SPOUserProfileProperty``.Department
Email = ``$SPOUserProfileProperty``.WorkEmail
WorkPhone = ``$SPOUserProfileProperty``.WorkPhone
Office = ``$SPOUserProfileProperty``.Office
JobTitle = ``$SPOUserProfileProperty``.``"SPS-JobTitle"
Responsibility = ``$SPOUserProfileProperty``.``"SPS-Responsibility"
Interests = ``$SPOUserProfileProperty``.``"SPS-Interests"
PictureURL = ``$SPOUserProfileProperty``.PictureURL
}
``$Results`` | Add-Member ``-MemberType`` `` MemberSet`` ``PSStandardMembers`` `` $PSStandardMembers
``$Results
}
}
}
``$ClientContext``.Dispose()
}
New-SPOList
This cmdlet is to create a New List or Library
Code
function`` `` New-SPOList
{
``<#
.Synopsis
This cmdlet is to create a New List or Library in the given site collection
.DESCRIPTION
This cmdlet is built using Client Side Object Model.
.EXAMPLE
New-SPOList -SPOUrl https://contoso.sharepoint.com -SPOListName LListName -SPOListType IssueTracking -SPOCredential SPOAdmin@contoso.onmicrosoft.com
.EXAMPLE
"List3" , "List4" | New-SPOList -SPOUrl https://contoso.sharepoint.com -SPOListType IssueTracking -SPOCredential SPOAdmin@contoso.onmicrosoft.com
#>
``Param``(
[``Parameter``(Mandatory=``$true``)]
[``uri``]``$SPOUrl``,
[``Parameter``(Mandatory=``$true``,
ValueFromPipeline=``$true``)]
[``string``]``$SPOListName``,
[``Parameter``(Mandatory=``$true``)]
[``Microsoft.SharePoint.Client.ListTemplateType``]``$SPOListType``,
[``Parameter``(Mandatory=``$true``)]
[``System.Management.Automation.CredentialAttribute``()]``$SPOCredential
)
``Process
{
``$ClientContext`` = [``Microsoft.SharePoint.Client.ClientContext``]::new(``$SPOUrl``)
``$ClientContext``.Credentials = [``Microsoft.SharePoint.Client.SharePointOnlineCredentials``]::new(``$SPOCredential``.UserName,``$SPOCredential``.Password)
``$Web`` = `` $ClientContext``.Web
``$ClientContext``.Load(``$Web``)
``$ClientContext``.ExecuteQuery()
``$NewApp`` = [``Microsoft.SharePoint.Client.ListCreationInformation``]::new()
``$NewApp``.Title = `` $SPOListName
``$NewApp``.TemplateType = [``int``]``$SPOListType
``$List`` = `` $Web``.Lists.Add(``$NewApp``)
``$ClientContext``.Load(``$List``)
``$ClientContext``.ExecuteQuery()
``$Web``.Update()
``$ClientContext``.Dispose()
}
New-SPOSiteCollection
This cmdlet is to create a new site collection in SharePoint Online
Code
function`` `` New-SPOSiteCollection
{
``<#
.Synopsis
This cmdlet retrieves all the List and Libraries from the given URL
.DESCRIPTION
This cmdlet is built using Client Side Object Model.
.EXAMPLE
New-SPOSiteCollection -SPOTenantAdminUrl "https://contoso-admin.sharepoint.com" -SPOWebTemplate <ValidValuesapperas> -SPOTitle <Title> -SPOSiteOwner <UPN> -SPOCredential <UPN>
.EXAMPLE
.EXAMPLE
.EXAMPLE
#>
``Param``(
[``Parameter``(Mandatory=``$true``)]
[``uri``]``$SPOTenantAdminUrl``,
[``Parameter``(Mandatory=``$false``,
HelpMessage=``"If template is not specified manually open the site and choose the one you need"``,
ValueFromPipeline=``$true``)]
[``ValidateSet``(``"STS#0"``,``"BLOG#0"``,``"BDR#0"``,``"DEV#0"``,``"OFFILE#1"``,``"EHS#1"``,``"BICenterSite#0"``,``"SRCHCEN#0"``,``"BLANKINTERNETCONTAINER#0"``,``"ENTERWIKI#0"``,``"PROJECTSITE#0"``,``"PRODUCTCATALOG#0"``,``"COMMUNITY#0"``,``"COMMUNITYPORTAL#0"``,``"SRCHCENTERLITE#0"``,``"visprus#0"``)]
[``string``]``$SPOWebTemplate``,
[``Parameter``(Mandatory=``$false``)]
[``string``]``$SPOTitle``,
[``Parameter``(Mandatory=``$true``)]
[``string``]``$SPOSiteOwner``,
[``Parameter``(Mandatory=``$true``,
ValueFromPipeline=``$true``)]
[``uri``]``$SPOSiteUrl``,
[``Parameter``(Mandatory=``$true``)]
[``System.Management.Automation.CredentialAttribute``()]``$SPOCredential
)
``$ClientContext`` = [``Microsoft.SharePoint.Client.ClientContext``]::new(``$SPOTenantAdminUrl``)
``$ClientContext``.Credentials = [``Microsoft.SharePoint.Client.SharePointOnlineCredentials``]::new(``$SPOCredential``.UserName,``$SPOCredential``.Password)
``$SPOTenant`` = [``Microsoft.Online.SharePoint.TenantAdministration.Tenant``]::new(``$ClientContext``)
``$Props`` = [``Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties``]::new()
``$Props``.Title = `` $SPOSiteTitle
``$Props``.Url = `` $SPOSiteUrl
``$Props``.Template = `` $SPOWebTemplate
``$Props``.Owner = `` $SPOSiteOwner
``$SPOTenant``.CreateSite(``$Props``) | Out-Null
``$ClientContext``.ExecuteQuery()
``$ClientContext``.Dispose()
}