This post is going to help you in learning how to do basic SharePoint List operations (CRUD – Create, Read, Update and Delete) using SharePoint Management Shell or Windows PowerShell.
Lets open SharePoint Management Shell or open Windows PowerShell.When we are working on SharePoint Server there are two possibilities exists: either select the SharePoint Management Shell or Open Windows PowerShell.If We are using SharePoint Management Shell then SharePoint snap-in will already be installed.If you are using Standard PowerShell console, we can install the snap-in by entering the following command:
Add-PSSnapIn Microsoft.SharePoint.PowerShell
we can check the list of installed snap-in by using this command
Get-PSSnapIn
So, here is my list like below screenshot.
1) Create Item into SharePoint List
$webURL="http://optimumview" $lstName="DemoList" $oWeb=Get-SPWeb -Identity $webURL $oList=$oWeb.Lists[$lstName] $oListItem = $oList.items.add() $oListItem["Title"]="Test-Item-0" $oListItem["DemoLocation"]="US" $oListItem["ProjectName"]="POW-JO-09" $oListItem.Update()
2) Read single list item
$webURL="http://optimumview" $lstName="DemoList" $oWeb=Get-SPWeb -Identity $webURL $oList=$oWeb.Lists[$lstName] #To read single item uncomment below code #$oListItem = $oList.GetItemById("1") #Read multiple list item $oListItem = $oList.Items | where {$_['ProjectName'] -like "IND1*"} if($oListItem -ne $null) { $oListItem | ForEach-Object { Write-Host $_['ID'] -foregroundcolor green Write-Host $_['Title'] -foregroundcolor green Write-Host $_['DemoLocation'] -foregroundcolor green Write-Host $_['ProjectName'] -foregroundcolor green } } else { Write-Host "No Items are found !!" -foregroundcolor red }
3) Update SharePoint List item
#Update multiple list item
$webURL="http://optimumview" $lstName="DemoList" $oWeb=Get-SPWeb -Identity $webURL $oList=$oWeb.Lists[$lstName] $oListItem = $oList.Items | where {$_['ProjectName'] -like "IND*"} if($oListItem -ne $null) { $oListItem | ForEach-Object { $_['Title']="Test1" + $_['ID'] Write-Host "Item id:" $_['ID'] " has been updated!" -foregroundcolor green $_.Update() } } else { Write-Host "No Items are found !!" -foregroundcolor red }
4) Deleting SharePoint List item
#Deleting multiple list item $webURL="http://optimumview" $lstName="DemoList" $oWeb=Get-SPWeb -Identity $webURL $oList=$oWeb.Lists[$lstName] $oListItem = $oList.Items | where {$_['ProjectName'] -like "GLT*"} if($oListItem -ne $null) { $oListItem | ForEach-Object { $_.Delete() Write-Host "Item id:" $_['ID'] " has been Deleted!" -foregroundcolor green } } else { Write-Host "No Items are found !!" -foregroundcolor red }