How To Read Column From CSV File In PowerShell

Introduction

In this article, we are going to learn how to read a column from CSV file in PnP PowerShell.

Use Case

While working with PowerShell scripting we used to have requirement to read column values from csv file and perform some operation. like bulk update item in SharePoint, Create site collection etc.

Here is the sample CSV file we have   -  SitesToCreate.csv

Columns - Site Title,SiteURL,Template,StorageQuota,Owner,TimeZone,LocId

Script to read CSV file

Using below powershell script code we can read the columns from csv file (SitesToCreate.csv)

PS C:\Sachin\ReadCSV> .\ReadCSV.ps1

[parameter(Mandatory=$false)][string]$CsvFilePath = ".\SitesToCreate.csv" #Loading CSV file

$csvItems = Import-Csv $CsvFilePath

foreach($Item in $csvItems)
{

Write-Host "Site Name-" $Item.SiteName -ForegroundColor Green
Write-Host "SiteURL-" $Item.SiteURL -ForegroundColor Green
Write-Host "Site Template-" $Item.Template -ForegroundColor Green
Write-Host "Site StorageQuota-" $Item.StorageQuota -ForegroundColor Green
Write-Host "Site Owner-" $Item.Owner -ForegroundColor Green
Write-Host "Site TimeZone-" $Item.TimeZone -ForegroundColor Green
Write-Host "Site Location Id-" $Item.LocId -ForegroundColor Green

}

Result-

PS C:\Sachin\ReadCSV> .\ReadCSV.ps1

SiteName : Devlopment Site

SiteURL : https://advanced365app.sharepoint.com/sites/DevTest

Template : STS#0

StorageQuota : 100

Owner : [email protected]

TimeZone : 4

LocId : 1033

SiteName : UAT Site

SiteURL : https://advanced365app.sharepoint.com/sites/UATSite

Template : STS#0

StorageQuota : 100

Owner : [email protected]

TimeZone : 4

LocId : 1033

Summary

In this article we can understand and learn how to read a column from csv file in PnP PowerShell.