SharePoint Online: PowerShell to Create a List View

SharePoint Online Views helps to organize columns together and present the data with sort, group and filtering capabilities. Views can be either Private or Public – A public view is available to all users, whereas a private view is available only to the user who created it. When you create a list or library it comes with default views E.g “All Items” with certain columns. You can create any number of views to support different use cases in the list or library.

How to Create View in SharePoint Online?

List views enable you to sort, filter, group, and format the data in your SharePoint list, so you can easily access it. To create a new view, do the following:

  •  Navigate to the list in which you want to create a new View in SharePoint Online.
  • Under List Tab, click on the “Create View” button from the Ribbon. On the next page, choose a view type such as “Standard view”, “Datasheet View”, “Gantt View”, etc.
    sharepoint online PowerShell to create list view
  • Provide the Name to your view, Select Fields to display in the view, set Sort and Filter options, and click on “OK” to create a view in SharePoint Online List.
    sharepoint online create view
Do not edit the default view unless its really required! Instead You can create a new per your requirements!

SharePoint Online: How to Create a New View in Modern Sites?

Creating new view in SharePoint modern experience makes is even more simpler!

  • Apply the filter to the view, add-remove columns to it, Sort-group by as you wish.
    add new view in sharepoint online
  • Once your view is ready, save the current changes as a “View” by Clicking on the Views Drop-down and then on “Save View as”.
    how to create a view in sharepoint online

SharePoint Online: PowerShell to Create a List View

Here is how to create a list view using PowerShell in SharePoint Online.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Custom function to Create List view in SharePoint Online
Function Create-ListView()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [String[]] $ViewFields,
        [Parameter(Mandatory=$true)] [string] $viewQuery,
        [Parameter(Mandatory=$false)] [string] $ItemLimit = "30",
        [Parameter(Mandatory=$false)] [string] $IsDefaultView="$True"

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Check if the View exists in list already
        $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
        if($NewView -ne $NULL)  
            Write-host "View '$ViewName' already exists in the List!" -f Yellow
            $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
            $ViewCreationInfo.Title = $ViewName
            $ViewCreationInfo.Query = $ViewQuery
            $ViewCreationInfo.RowLimit = $ItemLimit
            $ViewCreationInfo.ViewFields = $Viewfields 
            $ViewCreationInfo.SetAsDefaultView = $IsDefaultView

            #sharepoint online powershell create view
            $NewView =$List.Views.Add($ViewCreationInfo)
            Write-host "New View Added to the List Successfully!" -ForegroundColor Green  
    Catch {
        write-host -f Red "Error Adding View to List!" $_.Exception.Message

#Set parameter values
[email protected]("Title","IsActive","Priority") # Comma separated - Internal Names
$ViewQuery="<OrderBy><FieldRef Name='Title' /></OrderBy><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>1</Value></Eq></Where>"

#Call the function to Create View in the list
Create-ListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -ViewFields $ViewFields -ViewQuery $ViewQuery

SharePoint Online PnP PowerShell to Create List View

Here is the PnP PowerShell to create a view in SharePoint Online

#Config Variables
$SiteURL = ""
$ListName= "Projects"
$ViewName= "Active Projects"
$ViewFields = @("ProjectID", "Title","Category","ProjectStatus")
$Query = "<Where><Eq><FieldRef Name = 'ProjectStatus' /><Value Type = 'Choice'>Active</Value></Eq></Where>"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #sharepoint online pnp powershell create view
    Add-PnPView -List $ListName -Title $ViewName -ViewType Html -Fields $ViewFields -Query $Query -ErrorAction Stop
    Write-host "View '$ViewName' Created Successfully!" -f Green
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

Additionally, The Add-PnPView cmdlet supports -RowLimit, -SetAsDefault switches. You can use the Query parameter to set sorting or group by as well. E.g.:

  • Sort Order: “<OrderBy> <FieldRef Name= ‘Modified’ Ascending=’false’ /> </OrderBy>”
  • Group By: “<GroupBy><FieldRef Name = ‘Status’ /></GroupBy>”

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: PowerShell to Create a List View

  • November 24, 2021 at 8:13 AM

    Hi, thank you so much for this. Can we assign particular views to users? For example I want users A and B to see columns 1-10 and users C D E to be able to see columns 3-7. Is that possible?


Leave a Reply