Create New Custom List in SharePoint using PowerShell

Requirement: Add custom list to SharePoint using PowerShell.
create a sharepoint list using powershell

PowerShell to Create New List in SharePoint:
To create a new list or library in a SharePoint Online site, you must have the "Manage Lists" permission! The default permission levels Edit, Design, Manage Hierarchy, and Full Control contains this permission. So in order to create lists or libraries, you need to have at least one of these permission level. Here is the PowerShell script to create new list in SharePoint.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to create custom list
Function Create-List($SiteURL, $ListName)
{
    #Set the Error Action
    $ErrorActionPreference = "Stop"

    Try {
        $Web = Get-SPWeb -Identity $SiteURL
        $ListTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
       
        #Check if List with specific name exists
        if($Web.Lists.TryGetList($ListName) -eq $null)
        {
            $List = $Web.Lists.Add($ListName, $ListName, $ListTemplate)  
            write-host "List Created Successfully!" -ForegroundColor Green
        }
        else
        {
            write-host "List with specific name already exists!" -ForegroundColor Red
        }
    }
    catch {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    finally {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    }
}

#Parameters to create new List
$SiteURL="http://intranet.crescent.com/"
$ListName = "Customer Directory"

#Call the function to create new custom list
Create-List $SiteURL $ListName

This PowerShell script creates new custom list in SharePoint with "Title" column. If you want to add more columns to the list using PowerShell, Refer: How to Add Fields to SharePoint List using PowerShell.
Create New Custom List in SharePoint using PowerShell Create New Custom List in SharePoint using PowerShell Reviewed by Salaudeen Rajack on January 12, 2015 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.