惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
WordPress大学
WordPress大学
小众软件
小众软件
云风的 BLOG
云风的 BLOG
IT之家
IT之家
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
宝玉的分享
宝玉的分享
博客园 - Franky
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
D
Docker
博客园 - 聂微东
C
Check Point Blog
H
Help Net Security

博客园 - 霖雨

用PowerShell命令一次启动多个前后端应用 Visual Studio Code中配置Cline 解决MSSQLLocalDB连接失败的问题 使用App Registration 访问 SharePoint Online (二) 使用App Registration 访问 SharePoint Online (一) SharePoint Online 文档库的还原功能 SharePoint Online 快捷方式功能 SharePoint Online 文档库的收藏功能 使用豆包开发SharePoint Framework项目 Power Automate 还原误删的 Flow PowerShell 获取已删除的 Automate Flow PowerShell 新建 Address Book Policy PowerShell 批量添加用户到组 PowerShell 批量下载 SharePoint Online 文档 PowerShell 清空 SharePoint Online 列表数据 PowerShell 获取 SharePoint Online 站点信息 PowerShell 启用 SharePoint Online CDN SharePoint Online 定时发布页面 SharePoint Online 页面诊断 Microsoft Whiteboard 的使用 SharePoint Online 网站配置时区 SharePoint Online 集成 Viva Connections SharePoint Online 分区模板 SharePoint Online 嵌入 Power BI 报表
PowerShell 新建 SharePoint Online 列表
霖雨 · 2026-03-01 · via 博客园 - 霖雨

  前言

  最近,在开发SharePoint Online项目中,发现有好多好多好多的表要创建。

  正文

  1.这是PowerShell 执行成功的页面,如下图:

image

  2.这是建好的SharePoint Online 列表,非常的丝滑,如下图:

image

  3.PowerShell 命令,大家有需要可以改一下就可以用,尤其是封装一下customFields这个变量,就能通用,如下图:

<#
.SYNOPSIS
PowerShell 7专用 - 创建SharePoint Online自定义列表并批量添加字段
.DESCRIPTION
使用PnP.PowerShell创建自定义列表,支持配置列表基础信息,批量添加文本/数字/日期/布尔/多行文本等字段,兼容MFA登录
#>

# -------------------------- 可配置变量(按需修改以下内容) --------------------------
# 站点和列表基础信息
$siteUrl = "https://linyus.sharepoint.com/sites/Develop"  # 替换为目标SPO站点地址
$listName = "产品管理列表"                                      # 自定义列表名称
$listDescription = "用于存储公司产品基础信息的自定义列表"       # 列表描述(可选)

# 自定义字段配置数组(可增删/修改字段,支持常见类型)
# 支持的字段类型:Text(单行文本)、Number(数字)、DateTime(日期时间)、Boolean(是/否)、Note(多行文本)、Currency(货币)
$customFields = @(
    @{
        DisplayName = "产品编号"       # 字段显示名
        InternalName = "ProductCode"   # 字段内部名(无空格/特殊字符)
        Type = "Text"                  # 字段类型
        Required = $true               # 是否必填
    },
    @{
        DisplayName = "产品名称"
        InternalName = "ProductName"
        Type = "Text"
        Required = $true
    },
    @{
        DisplayName = "产品价格"
        InternalName = "ProductPrice"
        Type = "Currency"              # 货币类型
        Required = $true
        MinimumValue = 0               # 最小值(数字/货币类型有效)
    },
    @{
        DisplayName = "库存数量"
        InternalName = "StockCount"
        Type = "Number"
        Required = $false
    },
    @{
        DisplayName = "是否上架"
        InternalName = "IsOnShelf"
        Type = "Boolean"
        Required = $false
        DefaultValue = $false          # 默认值(布尔类型有效)
    },
    @{
        DisplayName = "发布日期"
        InternalName = "ReleaseDate"
        Type = "DateTime"
        Required = $false
        Format = "DateTime"            # 日期格式:DateTime(日期+时间)/Date(仅日期)
    },
    @{
        DisplayName = "产品描述"
        InternalName = "ProductDesc"
        Type = "Note"                  # 多行文本
        Required = $false
        RichText = $true               # 是否支持富文本(仅Note类型有效)
    }
)

# -------------------------- 核心执行逻辑 --------------------------
# 1. 检查并安装PnP.PowerShell模块(PowerShell 7兼容版)
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
    Write-Host "🔧 正在安装最新版PnP.PowerShell..." -ForegroundColor Cyan
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction SilentlyContinue
    Install-Module -Name PnP.PowerShell -Force -AllowClobber -Scope CurrentUser -ErrorAction Stop
}

# 2. 导入模块
Import-Module PnP.PowerShell -DisableNameChecking -Force -ErrorAction Stop
Write-Host "✅ PnP.PowerShell模块加载成功`n" -ForegroundColor Green

# 初始化连接状态
$isConnected = $false

try {
    # 3. 连接SPO站点(兼容MFA,PowerShell 7稳定登录方式)
    Write-Host "🔗 正在连接站点: $siteUrl" -ForegroundColor Cyan
    Connect-PnPOnline -Url $siteUrl -UseWebLogin -ErrorAction Stop
    $isConnected = $true
    Write-Host "✅ 站点连接成功`n" -ForegroundColor Green

    # 4. 检查列表是否已存在(避免重复创建)
    $existingList = Get-PnPList -Identity $listName -ErrorAction SilentlyContinue
    if ($existingList) {
        Write-Host "⚠️ 列表 '$listName' 已存在,跳过创建步骤" -ForegroundColor Yellow
    }
    else {
        # 5. 创建自定义列表(GenericList=自定义列表模板)
        Write-Host "📝 正在创建列表: $listName" -ForegroundColor Cyan
        New-PnPList -Title $listName -Template GenericList -ErrorAction Stop
        Write-Host "✅ 列表 '$listName' 创建成功`n" -ForegroundColor Green
    }

    # 6. 批量添加自定义字段
    Write-Host "➕ 开始添加自定义字段...`n" -ForegroundColor Cyan
    foreach ($field in $customFields) {
        # 检查字段是否已存在
        $fieldExists = Get-PnPField -List $listName -Identity $field.InternalName -ErrorAction SilentlyContinue
        if ($fieldExists) {
            Write-Host "⚠️ 字段 '$($field.DisplayName)' 已存在,跳过添加" -ForegroundColor Yellow
            continue
        }

        # 根据字段类型构建参数
        $fieldParams = @{
            List = $listName
            DisplayName = $field.DisplayName
            InternalName = $field.InternalName
            Type = $field.Type
            Required = $field.Required
            AddToDefaultView = $true  # 自动添加到默认视图
            ErrorAction = "Stop"
        }

        # 添加字段到列表
        Add-PnPField @fieldParams
        Write-Host "✅ 字段 '$($field.DisplayName)' 添加成功" -ForegroundColor Green
    }

    Write-Host "`n🎉 所有操作完成!" -ForegroundColor Green
    Write-Host "📌 站点地址: $siteUrl" -ForegroundColor Green
    Write-Host "📌 列表名称: $listName" -ForegroundColor Green
}
catch {
    Write-Host "`n❌ 执行出错: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}
finally {
    # 断开连接(仅在连接成功时执行)
    if ($isConnected) {
        try {
            Disconnect-PnPOnline -ErrorAction SilentlyContinue
            Write-Host "`n🔌 已断开SPO站点连接" -ForegroundColor Cyan
        }
        catch {
            Write-Host "`n⚠️ 断开连接时的小问题(可忽略): $($_.Exception.Message)" -ForegroundColor Yellow
        }
    }
}

  结束语

  其实没什么想多说的,大家看着用吧。