Introduction
Creating a SharePoint list using PowerShell with web login can greatly simplify your SharePoint administration tasks. This guide will walk you through the process, ensuring you can efficiently manage your SharePoint environment without needing to store admin credentials in your scripts.
Prerequisites
Before you start, ensure you have the following:
- PnP PowerShell Module installed.
- SharePoint Admin Center access.
- Admin or appropriate credentials for your SharePoint Online environment
Step-by-Step Guide
Step 1: Install the PnP PowerShell Module
If you haven’t already installed the PnP PowerShell module, you can do so using the following command:
Install-Module -Name PnP.PowerShell
Step 2: Connect to SharePoint Online Using Web Login
First, you need to connect to your SharePoint Online environment using your web login credentials.
# Connect to SharePoint Online
$SiteURL = "https://yourdomain.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $SiteURL -UseWebLogin
A web browser window will open prompting you to log in with your SharePoint credentials.
Step 3: Create a New SharePoint List
Once connected, you can create a new SharePoint list. Here’s an example of how to create a custom list.
# Define list properties
$ListTitle = "MyCustomList"
# Create the list
Add-PnPList -Title $ListTitle -Template GenericList -Description "A custom list created using PowerShell" -OnQuickLaunch
Write-Output "List '$ListTitle' created successfully."
Step 4: Add Columns to the List
To make your list more functional, you can add columns to it. Here’s how you can add different types of columns.
# Add a text column
Add-PnPField -List $ListTitle -DisplayName "MyTextField" -InternalName "MyTextField" -Type Text
# Add a number column
Add-PnPField -List $ListTitle -DisplayName "MyNumberField" -InternalName "MyNumberField" -Type Number
Write-Output "Columns added successfully."
Step 5: Verify the List and Columns
After creating the list and adding columns, it’s good to verify that everything is set up correctly.
# Retrieve and display list details
$list = Get-PnPList -Identity $ListTitle
Write-Output "List Title: $($list.Title)"
# Retrieve and display column details
$fields = Get-PnPField -List $ListTitle
$fields | ForEach-Object {
Write-Output "Field: $($_.Title), Type: $($_.FieldTypeKind)"
}
Conclusion
With these steps, you can quickly set up a new list, add columns, and customize it to meet your needs. PowerShell scripting allows for automation and consistency, making it an essential tool for SharePoint administrators.
No Comment! Be the first one.