Skip to content

Command Reference

Chrissy LeMaire edited this page Aug 2, 2018 · 8 revisions

Command summaries

In the examples below, I'll be connecting to my SharePoint 2016 server, aptly named https://sharepoint2016.

Connect-SPRSite

Creates a reusable SharePoint Client Context object that lets you use and manage the site collection in Windows PowerShell.

Connect-SPRSite -Site https://sharepoint2016

Disconnect-SPRSite

Disconnects a SharePoint Client Context object that lets you use and manage the site collection in Windows PowerShell.

Disconnect-SPRSite

Get-SPRConnectedSite

Returns the connected SharePoint Site Collection.

Get-SPRConnectedSite

Add-SPRColumn

Adds a column to a SharePoint list.

Add-SPRColumn -List 'My List' -ColumnName TestColumn -Description Awesome

Add-SPRListItem

Adds items to a SharePoint list from various sources. CSV, generic objects, exported lists.

# Add from an object - what's important is that the columns match up, so Title and TestColumn
# exist both in the object and the list.
$object = @()
$object += [pscustomobject]@{ Title = 'Hello'; TestColumn = 'Sample Data'; }
$object += [pscustomobject]@{ Title = 'Hello2'; TestColumn = 'Sample Data2'; }
$object += [pscustomobject]@{ Title = 'Hello3'; TestColumn = 'Sample Data3'; }

$object | Add-SPRListItem -List 'My List'
# You can even import from a SQL Server database. Note again the Title and TestColumn columns
Invoke-DbaSqlQuery -SqlInstance sql2017 -Query "Select Title = 'Hello SQL', TestColumn = 'Sample SQL Data'"  | 
Add-SPRListItem -List 'My List'

This is particularly cool. List doesn't exist? Auto-create it! Note, it mostly defaults to text rows so use this sparingly.

Invoke-DbaSqlQuery -SqlInstance sql2017 -Query "Select Title = 'Hello SQL',TestColumn = 'Sample SQL Data'"  | 
Add-SPRListItem -List BrandNewList -AutoCreateList

Update-SPRListData

Updates modified items in a SharePoint list.

# Update 'My List' from modified rows contained within C:\temp\mylist-updated.xml
$updates = Import-CliXml -Path C:\temp\mylist-updated.xml
Get-SPRListItem -List 'My List' | Update-SPRListItem -UpdateObject $updates -Confirm:$false

Clear-SPRListItems

Deletes all items from a SharePoint list.

# Delete all rows, clearing the list. This will prompt for confirmation.
Clear-SPRListItems -List 'My List'

# Positive you're deleting the rows you want? Add -Confirm:$false to avoid confirmation prompts.
Clear-SPRListItems -List 'My List' -Confirm:$false

Export-SPRListItem

Exports all items from a SharePoint list to a file.

# Export an entire list
Export-SPRListItem -List 'My List' -Path C:\temp\mylist.xml

# Export only some items
Get-SPRListItem -List 'My List' | Where Title -match Hello2 | Export-SPRListItem -Path C:\temp\hello2.xml

Get-SPRColumnDetail

Returns information (Name, DisplayName, Data type) about columns in a SharePoint list.

Get-SPRColumnDetail -List 'My List'

Get-SPRList

Returns a SharePoint list object.

Get-SPRList -List 'My List'

Get-SPRListItem

Returns data from a SharePoint list.

# Get the entire list
Get-SPRListItem -List 'My List'

# Get only item 1. You could also get -Id 1, 2, 3 and so on.
Get-SPRListItem -List 'My List' -Id 1

Get-SPRListTemplate

Get list of SharePoint templates.

Get-SPRListTemplate

Import-SPRListItem

Imports all items from a file into a SharePoint list.

# Manually specify the path to the xml file, which was exported from Export-SPRListItem
Import-SPRListItem -List 'My List' -Path C:\temp\mylist.xml

# Or pipe it in
Get-ChildItem C:\temp\mylist.xml | Import-SPRListItem -List 'My List' 

# You can even automatically create a list if it doesn't exist
dir 'C:\temp\My List.xml' | Import-SPRListItem -List Test -AutoCreateList

New-SPRList

Creates a new SharePoint list.

# Create a generic list with a description
New-SPRList -List List1 -Description "My awesome list"

# Create a document library
New-SPRList -List 'My Documents' -Template DocumentLibrary

Remove-SPRList

Deletes lists from a SharePoint site collection.

# Delete the list and prompt for confirmation.
Remove-SPRList -List List1

# Positive you're deleting the list you want? Add -Confirm:$false to avoid confirmation prompts.
Remove-SPRList -List List2 -Confirm:$false

Remove-SPRListItem

Deletes items from a SharePoint list.

# Delete a couple items and prompt for confirmation.
Get-SPRListItem -List 'My List' -Id 44, 45 | Remove-SPRListItem

# Delete a bunch of items without confirmation.
Get-SPRListItem -List 'My List' | Where Title -match Hello | Remove-SPRListItem -Confirm:$false

Select-SPRObject

Makes it easier to alias columns to select and rename for export.

# Get two columns, FullName and Created from 'My List'. In the example below, FullName is an alias of Title.
# This makes it easy to import items.xml to a SharePoint with with a FullName column.

Get-SPRListItem -Site intranet.ad.local -List 'My List' | Select-SPRObject -Property 'Title as FullName', Created | Export-SPRObject -Path C:\temp\items.xml

Copy-SPRFile

Copy a file using robocopy or Bits Transfer.

## Exports a list to a file then copies it to \\nas\dropoff using robocopy
Export-SPRListItem -List MyList -Path C:\temp\mylist.dat | Copy-SPRFile -Destination \\nas\dropoff

## Exports a list to a file then copies it using Start-BitsTransfer to \\nas\dropoff with the credential ad\user
Export-SPRListItem -List MyList -Path C:\temp\mylist.dat | Copy-SPRFile -Destination \\nas\dropoff -Credential ad\user -Method BitsTransfer
Clone this wiki locally