2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PowerShell】PnP.PowerShell を使用して SharePoint Online のリストを操作する

Last updated at Posted at 2021-09-02

PnP.PowerShell を使用してリストを操作します。


関連記事


準備

Module のインストール

PowerShell Gallery からModule をインストールします。

PnP.PowerShell

# PnP PowerShell のインストールと更新
if((Get-Module PnP.PowerShell -ListAvailable | measure).Count -eq 0) {
    Install-Module PnP.PowerShell -Force
} else {
    Update-Module PnP.PowerShell
}
Get-Command -Module PnP.PowerShell | select Name, Version

注意 2024/9/9 現在
PnP PowerShell で使用する独自の EntraID アプリケーションを登録することが必須のステップになりました。

Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnPXXX(任意の名前でOK)" -Tenant [yourtenant].onmicrosoft.com -Interactive

登録されたアプリケーションの Client Id が出力されます。
アプリケーションの Client Id は接続時に指定しますので、こちらは控えておきましょう。

操作

SharePoint Web サイトの操作

サイトに接続する

登録したアプリケーションの Client Id が必要になります。

# SharePoint サイトの URL
$SiteUrl = "https://${Tenant}/sites/$SiteName"
# 資格情報取得
$Credentials = Get-Credential
# SharePoint 管理センターに接続
Connect-PnPOnline -Url $SiteUrl -Credentials $Credentials -ClientId $ClientId

SharePoint リストの操作

すべての SharePoint リストを取得する

Get-PnPList

リストを作成および更新する

# カスタムリスト作成
$ListName = 'Announcements'
$newList = New-PnPList -Title $ListName -Template GenericList

Set-PnPList -Identity $ListName -Description 'New Description'

リストを削除する

$ListName = 'Announcements'
Remove-PnPList -Identity $ListName

リストからアイテムを取得する

# リストアイテム情報取得
$ListName = 'Announcements'
$Id = 2
Get-PnPListItem -List $ListName -Id $Id

新しいリストアイテムを作成する

$ListName = 'Announcements'
Add-PnPListItem -List $ListName -Values @{ Title='My New Item!'; Body='Hello World!' }

リストアイテムに添付ファイルを追加する

$item = Get-PnPListItem -List $ListName -Id $Id
[System.IO.FileInfo] $file = 'C:\Data\test.txt'
$at = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
$at.FileName =  $file.Name
$at.ContentStream = $file.OpenRead()
$item.AttachmentFiles.Add($at)
Invoke-PnPQuery

参考サイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?