-
Notifications
You must be signed in to change notification settings - Fork 10
Command Reference
In the examples below, I'll be connecting to my SharePoint 2016 server, aptly named https://sharepoint2016
.
These are the commands you'll probably use most often.
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-DbaQuery -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-DbaQuery -SqlInstance sql2017 -Query "Select Title = 'Hello SQL',TestColumn = 'Sample SQL Data'" |
Add-SPRListItem -List BrandNewList -AutoCreateList
Uploads a file to a SharePoint list.
# Uploads README.md to the Sup list on sharepoint.ad.local, skips the file if the destination file if it exists.
Get-ChildItem .\README.md | Add-SPRListItemFile -Site sharepoint.ad.local -List Sup
# Uploads README.md to the Sup list on sharepoint.ad.local, overwrites the destination file if it exists
Get-ChildItem .\README.md | Add-SPRListItemFile -Site sharepoint.ad.local -List Sup -Overwrite
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
Creates a reusable SharePoint Client Context object that lets you use and manage the site collection in Windows PowerShell.
Connect-SPRSite -Site https://sharepoint2016
Copy the compressed 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
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
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
Saves items from a SharePoint list to a file.
# Saves all files (attachments/documents) from My List on intranet.ad.local to C:\temp\
Save-SPRListItemFile -Site intranet.ad.local -List 'My List' -Path C:\temp
# Saves files (attachments/documents) from My List matching cupcake from intranet.ad.local to C:\temp\
Get-SPRListItem -List 'My List' -Site intranet.ad.local | Where Title -match 'cupcake' | Save-SPRListItemFile -Path C:\temp\
Updates modified items in a SharePoint list.
# Update 'My List' from modified rows contained within C:\temp\mylist-updated.xml
# Prompts for confirmation
# Matches are based on GUID
$updates = Import-CliXml -Path C:\temp\mylist-updated.xml
Get-SPRListItem -List 'My List' | Update-SPRListItem -UpdateObject $updates -Confirm:$false
# Update 'My List' from modified rows contained within C:\temp\mylist-updated.xml
# Does not prompt for confirmation
# Matches are based on column SSN
$updates = Import-CliXml -Path C:\temp\mylist-updated.xml
Get-SPRListItem -List 'My List' -Site intranet.ad.local | Update-SPRListItem -UpdateObject $updates -KeyColumn SSN -Confirm:$false
These commands are also useful and worth a perusal.
Adds a column to a SharePoint list.
Add-SPRColumn -List 'My List' -ColumnName TestColumn -Description Awesome
Disconnects a SharePoint Client Context object that lets you use and manage the site collection in Windows PowerShell.
Disconnect-SPRSite
Returns the connected SharePoint Site Collection.
Get-SPRConnectedSite
Returns information (Name, DisplayName, Data type) about columns in a SharePoint list.
Get-SPRColumnDetail -List 'My List'
Returns a SharePoint list object.
Get-SPRList -List 'My List'
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 list of SharePoint templates.
Get-SPRListTemplate
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
Creates a new SharePoint list used for logging (-LogToList in supported commands)
# Create a list called SPReplicator (use -Title to name it something else) on intranet.ad.local
New-SPRLogList
# Create a logging list titled Logger then logs the results from Import-SPRListItem
New-SPRLogList -Site intranet.ad.local -Title Logger
$loglist = Get-SPRList -List Logger
Import-SPRListItem -Site intranet.ad.local -List 'My List' -Path C:\temp\mylist.dat -LogToList $loglist
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
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
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
Updates author (created by) of items in a SharePoint list.
# Update the author and editor for all items in My List on intranet.ad.local to ad\newuser. Prompts for confirmation.
Get-SPRListItem -List 'My List' -Site intranet.ad.local | Update-SPRListItemAuthorEditor -Identity ad\newuser
# Update just the editor for all items in My List on intranet.ad.local to ad\newuser. Does not prompt for confirmation
Get-SPRListItem -List 'My List' -Site intranet.ad.local | Update-SPRListItemAuthorEditor -Identity ad\newuser -Column Editor -Confirm:$false