Creating a Custom PowerShell Function for SharePoint

In previous articles I showed you how to develop custom PowerShell cmdlets using Visual Studio. In this article we will cover how one can go about creating custom function using native PowerShell scripts. The method we will create will accept piping parameters to it in order to fully leverage the power of PowerShell. To demonstrate this, we will go and create a new PowerShell function name Get-SPListItemCount that will receive and SPList object as an input parameter, and will output a custom PowerShelll object containing two parameters: the list title, and the number of items contained within it.

To begin our example, let’s have a look at what our code will look like for the function. The following block of PowerShell script represent the logic of our function (very basic logic):

Function Get-SPListItemCount

{

param(

[Parameter(Mandatory=$true,

ValueFromPipeline=$True)]

$list

)

[PSCustomObject]@{‘List’ = $list.Title; ‘Item Count’ = $list.ItemCount}

}

From the code above, we start off by declaring function Get-SPListItemCount. Then to start off the function declaration, we specify what parameters our method will be accepting, in our case a single parameter, named $list which represents the received SPList object for which we wish to obtain the item count. Using the Parameter, we specify three very important settings. First, that the parameter is mandatory, meaning we require the user the pass in at least one parameter, second that the parameter can be received by the pipeline (using the PowerShell ‘|’ operator), and third that the parameter received will be assigned to the $list variable within the function.

Then using the [PSCustomObject] operator, we ensure that our function returns a custom PowerShell object that will contain two properties: the Title of the list received as a parameter, and the ItemCount property of that same list which represents the total number of SPList item contained within the list.

Putting it all together, we can then call into our custom function throughout our script in the following manner:

$web = Get-SPWeb http://localhost

$web.Lists[“Documents”] | Get-SPListItemCount

The example above obtains a reference to the “Documents” library located at the rootweb (http://localhost), then passes it to our custom Get-SPListItemCount method using the pipeline operator. The following figure shows the result of executing our script using PowerShell:

20150416-01.png

Microsoft Premier Field Engineer - SharePoint

2 thoughts on “Creating a Custom PowerShell Function for SharePoint

  1. Neel

    Nik,

    Good article.. I have issue, hope you can guide me through.. I have a pdf file in a document library, and the pdf file has some attachments (icons) and their urls that needs to be modified, I am not sure how.. I tried list items it works fine, in my case I need to traverse the pdf file content to replace and update the url

    hope you can guide me
    Thank you
    Neel

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*