How to read SharePoint list using PowerShell

For long time I wanted to dive a bit deeper into PowerShell. Hopefully I will be able to post few of the sharepoint powershell tricks here.
First one is about getting list items from a sharepoint list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

cls
if[[Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}] -eq $null] {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "//sharepointsite"
$sourceListName = "mylist"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#$spSourceItems = $spSourceList.GetItems[]
#$spSourceItems = $spSourceList.GetItemById["1"]
$spSourceItems = $spSourceList.Items | where {$_['ID'] -eq 1}

$spSourceItems | ForEach-Object {
Write-Host $_['ID']
Write-Host $_['Title']
}

You can either get all the items with GetItems[] method currently commented out or you can filter items by some parameters. Filter applied in the script above is also equal to the GetItemById[1] method.

Video liên quan

Chủ Đề