0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】CSV ファイルの内容をカスタムリストのリストアイテムとして追加する

Posted at

CSV ファイルの内容を読み込んで、新規でカスタムリストを作成し、リストアイテムを追加する方法です。


関連ページ


カスタムリストを作成する

CSV ファイル名のカスタムリストを作成します。

using namespace System.IO;

# CSV ファイル情報
[FileInfo] $csv = '.\Announcements.csv'

# カスタムリスト作成
$newList = New-PnPList -Title $csv.BaseName -Template GenericList

[string] $ListName = $newList.Title

リストに列を追加する

# CSV ファイルインポート
$Items = ipcsv $csv

# 列情報取得
$Header = $Items[0] | Get-member -MemberType 'NoteProperty' | select Name

# 列追加
$Header | % {
    $colName = $_.Name
    if (!(Get-PnPField -List $ListName -Identity $colName 2>$null)) {
        Add-PnPField -List $ListName -DisplayName $colName -InternalName $colName -Type Text -AddToDefaultView | Out-Null
    }
}

リストアイテムを追加する

# CSV 取り込み
$Items | % {
    $Item = $_
    $hashtable = @{}
    foreach($property in $Item.psobject.Properties.name) {
        $hashtable[$property] = $Item.$property
    }
    Add-PnPListItem -List $ListName -Values $hashtable
}

参考サイト

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?