Installing the cmdlets

  1. You would need Windows7 or above. PowerShell V3 is a pre-requisite.
  2. Download and extract the contents of the .ZIP to a folder on your disk
  3. Click on launch.bat to start a new PowerShell console session with the relevant module imported.

 

Cmdlets supported in this release

Security

  1. Get-SPGroups - Returns all security groups in the site collection along with owner information
  2. Get-SPSiteUsers - Returns all all users in the site collection
  3. Get-SPGroupMembers - Returns all users who are members in the specified groups
  4. Add-SPGroup - Creates a new security group
  5. Add-SPGroupUser - Adds an user to the specified SharePoint group
  6. Remove-SPGroup - Deletes a security group
  7. Get-SPGroupsFromUser - Returns all the groups where the user is a member
  8. Get-SPSiteUsers  - Gets all users from a site collection
  9. Get-SpSiteUser - Gets a single user from a site collection
  10. Update-SPSiteUser - Updates display name and email address of an user in the site collection
  11. Remove-SPGroupUser - Removes the specified user from the site collection

Working with Lists and document libraries

  1. Get-SPLists - gets a collection of Lists in a sub-site
  2. Get-SPList - gets the detailed properties of a List
  3. Get-SPWebs - gets a collection of sub-sites. Can be recursive.
  4. Get-SPWeb - gets information about a site
  5. Get-WebUrlFromAnyUrl - Returns the absolute URL to a sub-site when supplied with the absolute URL to any object in this site. 

Working with List items and documents

  1. Add-SPListItem - Creates a new item in the specified List and with any specified custom meta-data.
  2. Add-SPFile - Creates a new document in the specified document library using the contents of a local file and also updates any custom meta-data
  3. Get-SPListItems - Returns a collection of list item from the specified List and web site with specified view fields. You can specfify a CAML query or specify an upper and lower ID range.
  4. Update-SPListItems - Updates the list item with the specified ID,List and web site using the specified property names and their values
  5. Remove-SPListItems - Deletes the list item with the specified ID,List and web site
  6. Checkin-SPFile - checkins the document with the specified URL
  7. Update-HttpFile - updates the contents of an existing document at the specified URL with a new file

 

 Get-SPGroups

Sample code

$url="http://server/sites/sitea"
get-spgroups -url $url

Sample output:

ID : 3
Name : Team Site Owners
Description : Use this group to gran
OwnerID : 3
OwnerIsUser : False
OwnerName : Team Site Owners
ID : 4
Name : Team Site Visitors
Description : Use this group to gran
OwnerID : 3
OwnerIsUser : False
OwnerName : Team Site Owners
 

 

Get-SPSiteUsers

Sample code

$url="http://server/sites/sitea"
get-spsiteusers -url $url 

Sample output:

ID : 1
Name : lastname1,firstname1
LoginName : SOMEDOMAIN\login1
Email : someone@example.com
Notes :
IsSiteAdmin : True
IsDomainGroup : False
Sid : S-1-5-21-3514878845-1463311699-2628523914-500
 

Get-SPGroupMembers

Sample code

    $url="http://server/sites/sitea"
    get-spgroupmembers -url $url -name "team site members"

Sample output

ID            : 1
Name          : WIN-BAD47L79T39\administrator
LoginName     : WIN-BAD47L79T39\administrator
Email         : someone@example.com
Notes         :
IsSiteAdmin   : True
IsDomainGroup : False
Sid           : S-1-5-21-3514878845-1463311699-2628523914-500

ID            : 7
Name          : WIN-BAD47L79T39\user1
LoginName     : WIN-BAD47L79T39\user1
Email         :
Notes         :
IsSiteAdmin   : False
IsDomainGroup : False
Sid           : S-1-5-21-3514878845-1463311699-2628523914-1011

Add-SPGroup

Sample code

$url="http://server/sites/sitea"
add-spgroup -url $url -group "group001" -description "Sample group" -ownergroup "Team Site Owners"

Get-SPLists (Basic usage)

Sample code

$url="http://server/sites/sitea"
get-splists -url $url

Sample output

ID             : a4b6d89a-7a4f-41b8-b150-a3cd4bfdf5e5
Name           : Announcements
ItemCount      : 1
DefaultViewUrl : /Lists/Announcements/AllItems.aspx
Url            : http://win-bad47l79t39

ID             : c22f5ca4-7a0e-4fe5-91cd-e116460e391f
Name           : Calendar
ItemCount      : 0
DefaultViewUrl : /Lists/Calendar/calendar.aspx
Url            : http://win-bad47l79t39

ID             : 5254583f-6900-4244-a776-2cf4609729d6
Name           : Links
ItemCount      : 0
DefaultViewUrl : /Lists/Links/AllItems.aspx
Url            : http://win-bad47l79t39

Get-SPLists (Display additional list properties)

Sample code

$url="http://server/sites/sitea"
get-splists -url $url -properties RequireCheckout,EnableVersioning -recursive $false
#
#The find out what are the properties that can be specified through the -properties parameter, please dump the contents of the raw List schema to disk and then examine the file
e.g.
get-splist -url $url -list Links -dump $true -file .\MyLinks.xml.schema
#The attributes in the List XML tag of the resulting file Links.xml.schema can be specified in the -properties parameter

Sample output

ID               : a4b6d89a-7a4f-41b8-b150-a3cd4bfdf5e5
Name             : Announcements
ItemCount        : 1
DefaultViewUrl   : /Lists/Announcements/AllItems.aspx
Url              : http://win-bad47l79t39
RequireCheckout  : False
EnableVersioning : False

ID               : c22f5ca4-7a0e-4fe5-91cd-e116460e391f
Name             : Calendar
ItemCount        : 0
DefaultViewUrl   : /Lists/Calendar/calendar.aspx
Url              : http://win-bad47l79t39
RequireCheckout  : False
EnableVersioning : False

ID               : 5254583f-6900-4244-a776-2cf4609729d6
Name             : Links
ItemCount        : 0
DefaultViewUrl   : /Lists/Links/AllItems.aspx
Url              : http://win-bad47l79t39
RequireCheckout  : False
EnableVersioning : False

Get-SPWebs

Sample code

$url="http://server/sites/sitea" -recursive $false
get-spwebs -url $url  -recursive $false

Sample output:

Title                                                       Url
-----                                                       ---
subsite01                                                   http://win-bad47l79t39/My Subsite 01
subsite02                                                   http://win-bad47l79t39/My Subsite 02

Get-WebUrlFromAnyUrl

Sample code

$url="http://server/sites/sitea"
get-weburlfromanyurl -url "http://win-bad47l79t39/My%20Subsite%2002/default.aspx"

Sample output

http://win-bad47l79t39/My Subsite 02

Get-SPGroupsFromUser

Sample code

$url="http://server/sites/sitea"
get-spgroupsfromuser -url $url -login "$env:USERDOMAIN\user1"

Sample output

ID          : 3
Name        : Team Site Owners
Description : Use this group to grant people full control permissions
OwnerID     : 3
OwnerIsUser : False
OwnerName   : Team Site Owners

Get-SPlistItems (fetch all items with ID between upper and lower limit)

Sample code

$url="http://server/sites/sitea"
get-splistitems -url $url -list Announcements -properties Title,Body -idmin 1 -idmax 2

Sample output

ID               : 1
Name             : 1_.000
Created          : 2014-09-07T18:26:12Z
Modified         : 2014-09-07T18:26:12Z
_UIVersion       : 512
_UIVersionString : 1.0
FSObjType        : 1;#0
File_x0020_Size  :
Title            : Get Started with Microsoft SharePoint Foundation!
EncodedAbsUrl    : http://win-bad47l79t39/Lists/Announcements/1_.000
FileLeafRef      : 1;#1_.000
Body             : <div class="ExternalClass5F83EEF4963345CFA6B6D9E30721F4D6">Microsoft SharePoint Foundation helps
                   you to be more effective by connecting people, information, and documents. For information on
                   getting started, see Help.</div>

ID               : 2
Name             : 2_.000
Created          : 2014-09-13T01:10:03Z
Modified         : 2014-09-13T01:10:03Z
_UIVersion       : 512
_UIVersionString : 1.0
FSObjType        : 2;#0
File_x0020_Size  :
Title            : Hello World Title
EncodedAbsUrl    : http://win-bad47l79t39/Lists/Announcements/2_.000
FileLeafRef      : 2;#2_.000
Body             : <div class="ExternalClass09C978F9D32B47BF88B0B6C81663E074"><p>Body of this anncouncement. You can
                   write rich text here​</p></div>

Get-SPlistItems (fetch all items by specifying a caml query)

Sample code

$url="http://server/sites/sitea"
$query="<Where><Lt><FieldRef Name='ID'/><Value Type='Counter'>144</Value></Lt></Where>"
get-splistitems -url $url -list Tasks -caml $query -properties Title,Status

Sample output

ID               : 1
Name             : 1_.000
Created          : 2014-09-16 11:07:53
Modified         : 2014-09-16 12:31:13
_UIVersion       : 512
_UIVersionString : 1.0
FSObjType        : 1;#0
File_x0020_Size  :
Title            : task 0001
EncodedAbsUrl    : http://win-bad47l79t39/Lists/Tasks/1_.000
FileLeafRef      : 1;#1_.000
Status           : Not Started

ID               : 2
Name             : 2_.000
Created          : 2014-09-16 11:08:01
Modified         : 2014-09-16 11:08:01
_UIVersion       : 512
_UIVersionString : 1.0
FSObjType        : 2;#0
File_x0020_Size  :
Title            : task 002
EncodedAbsUrl    : http://win-bad47l79t39/Lists/Tasks/2_.000
FileLeafRef      : 2;#2_.000
Status           : Not Started

Get-SPlistItems (fetch all items in a subfolder)

Sample code

$url="http://server/sites/sitea"
get-splistitems -url $url -list "Shared Documents" -IDMIN 1 -IDMAX 5000 -properties Title,Status -folder "Shared Documents/My folder/Sub folder1" -recursive $false

Add-SPListItems

Sample code

$url="http://server/sites/sitea"
$newitem=add-splistItem -url $url -list Announcements -properties Title,Body -values cooltitle,"Hello world"
#
#display the values of the newly created list item
#
$newitem.id,$newitem.title,$newitem.body

Sample output

3
cooltitle
<div class="ExternalClassA29661C3BF2C4AB3BE671A7BBFCC521A">Hello world</div>

Remove-SPListItem

Sample code

$url="http://server/sites/sitea"
remove-splistitem -url $url -list AnnounceMents -id 3

Sample output

 

Update-SPListItem

Sample code

$url="http://server/sites/sitea"
$listitem=update-splistItem -list Announcements -url $url -properties title,body -values cooltitle,"hello world" -id 4
$listitem.id,$listitem.Title,$listitem.body

Sample output

4
cooltitle
<div class="ExternalClassD2E91389FE984F44A8C240997B063C22">hello world</div>

Add-SPFile 

Sample code

$url="http://server/sites/sitea"
add-spfile -list "MyStatusReports" -url $url -properties ReportWeek,Title -values "2014-09-13T10:51:27z","My Status report for MIS project"

Sample output

to be done

Get-SPList (fetch List properties only)

Sample code

$url="http://server/sites/sitea"
get-splist -list "MyStatusReports" -url $url 

Sample output

AllowDeletion                 : True
AllowMultiResponses           : False
AnonymousPermMask             : 0
Author                        : 1073741
BaseType                      : 0
Created                       : 2014090
DefaultViewUrl                : /Lists/
Description                   : Use the
Direction                     : none
DocTemplateUrl                :
EmailAlias                    :
EnableAttachments             : True
EnableFolderCreation          : False
EnableMinorVersion            : False
EnableModeration              : False
EnablePeopleSelector          : False
EnableResourceSelector        : False
EnableVersioning              : False
EnforceDataValidation         : False

Get-SPList (fetch List properties and all fields)

Sample code

$url="http://server/sites/sitea"
$l=get-splist -list "MyStatusReports" -url $url -allfields
$l.Fields

Sample output

ID           : {c15b34c3-ce7d-490a-b133-3f4de8801b76}
DisplayName  : Status
InternalName : Status
ReadOnly     : False
Type         : Choice
FromBaseType : False

ID           : {d2311440-1ed6-46ea-b46d-daa643dc3886}
DisplayName  : % Complete
InternalName : PercentComplete
ReadOnly     : False
Type         : Number
FromBaseType : False

ID           : {53101f38-dd2e-458c-b245-0c236cc13d1a}
DisplayName  : Assigned To
InternalName : AssignedTo
ReadOnly     : False
Type         : User
FromBaseType : False

ID           : {50d8f08c-8e99-4948-97bf-2be41fa34a0d}
DisplayName  : Task Group
InternalName : TaskGroup
ReadOnly     : False
Type         : User
FromBaseType : False



Create a folder in a document library

Sample code

$url="http://server/sites/sitea/"
add-splistitem -url $url -list "Shared Documents" -properties FSObjType,BaseName -values 1,"My folder"

add-splistitem -url $url -list "Shared Documents" -properties FSObjType,BaseName -values 1,"My folder/subfolder1"
add-splistitem -url $url -list "Shared Documents" -properties FSObjType,BaseName -values 1,"My folder/subfolder1/subfolder2"
 

Downloading a single document from a document library whose URL is known in advance

Sample code

#
#In this example we are using the out of box PS cmdlet Invoke-WebRequest to download
#a file from a document library.
#
#$url="http://server/sites/sitea/shared documents/file1.docx"
$newFilePath="c:\\file1.docx"
Invoke-WebRequest -Uri $urldecoded -OutFile $newFilePath -UseDefaultCredentials

 

 

Downloading documents from a document library using a CAML query

Sample code

#
#In this example, we are fetching all list items using a CAML query 
#and then iterating through all the items and downloading the files to a local folder
#
$url="http://server/sites/sitea/"
$list="Documents"
$query="<Where><Lt><FieldRef Name='ID'/><Value Type='Counter'>200</Value></Lt></Where>"
$listItems=get-splistitems -list $list -url $url -query $query
foreach($item in $listItems)
{
$objtype=$item.FSObjType
$fileName=$item.Name
$url=$item.EncodedAbsUrl
if ($objtype.contains(";#1"))
{
Write-Host "Skipping because this is a folder $fileName"
continue;
}
$newFilePath=Join-Path -Path "c:\\temp" -ChildPath $fileName
Write-Host "Downloading file $fileName to $newFilePath"
Invoke-WebRequest -Uri $url -OutFile $newFilePath -UseDefaultCredentials
}



 

FAQ

How do you handle Date types while updating list items using Update-SPListItem?

#
#SharePoint web services expects the dates to be in ISO format.
#
$d=get-date
$datestring=$datestring=$dt.ToString("s")+"Z"
$url="http://server/sites/sitea"spfile -list "MyStatusReports" -url $url -properties ReportWeek,Title -values $datestring,"My Status report for MIS project"

How do you handle lookup columns while updating list items using

Update-SPListItem?

TO BE DONE

How do you update an existing document in a document library?

$url="http://server/sites/sitea/Shared Documents/SomeDoc.docx"
Update-HttpFile -$url -file "c:\\hello.docx"

 

Last edited May 15, 2015 at 3:58 PM by harappa, version 31