Part 4 – Getting Started With CSOM | SharePoint Online | PowerShell


We discussed and show cased few PowerShell and CSOM functionalities in earlier blogs. Please refer to the links below

In Part 3 we showed the steps to add Format files for our Script module and in this article we will demo an alternate method to Add custom view for our objects without creating PSOject explicitly and this is just a tip to get List Property information. Do remember we are using CSOM and PowerShell and there are high chances of performance issues which will be addressed in later upcoming blog articles. For, now we will cover alternate options.

Code at Ease

What we did here is a very simple and ugly way of getting List Information and created a default view using Format.ps1xml. The advantage is we will allow users to select the required properties! So, no need to type all properties names and keeps the code neat and clean. Again! Please ignore the performance we will discuss about it soon. This function takes maximum 4 seconds and 1 second minimum to query.


333 Well! It looks good for now! Let’ test it with different properties



Part 3 – Getting Started With CSOM | SharePoint Online | PowerShell


In our last blog series we have seen few concepts of PowerShell CSOM like creating functions and including comments based help for our PowerShell functions in the module. Reference Links is below

In this article we will demo about adding format files to get desired output.

What is Format File?

Why we need to add format file? The very simple reason is to define display the object in command line. For example check the output of Get-Service cmdlet. By default it shows the properties like Status, Name and DisplayName which is defined in the file DotNetTypes.Format.ps1xml. Please use this for reference and do not modify it to avoid tampering of your existing view in PowerShell. So, when we run Get-Service we get three properties in output by default and to see more we either do one of the below as required
111. Okay, with this reference we will create our own Format File and name it as SPOnlineModule.Format.PS1XML and the location is under our module folder. To make this article easy I am opting to make List View Basic

Get Started

Let’s make our job easy! I copied the code from the List View Basic and modified my PowerShell function to meet the requirement.

All we need to do is simple steps

  • Create a Format File and name it as SPOnlineModule.Format.ps1xml
  • Save the file inside your module directory
  • Include this file name in PSD1 file at FormatsToProcess = @(‘SPOnlineModule.Format.ps1xml’)
  • Import the Module
  • Connect to SharePoint Online and execute Get-xSPOList cmdlet which yields only two properties Title and ItemCount.

Just as an example I am selecting first 5 list and the output is illustrated below
Note: We haven’t fixed the Connect-xSPOTenant cmdlet yet! We need to add Conditional Scope to make it more friendly.

In our Get-xSPOList cmdlet I included the below code which appends the view name to the PSCustomObject


Part 2 – Getting Started With CSOM | SharePoint Online | PowerShell


In our last blog post (Part 1) we have show cased two functions which allows us to connect to SharePoint Online and retrieves list information from the site collections. Now, we will create a small PowerShell module with help.

About Modules

To learn about PowerShell Modules refer this link. We will be focusing about Script Module! Which is easy to understand and to troubleshoot as well. A script Module is nothing but valid script saved as .psm1 file and it has a structure to save. For our demo, we will save the PowerShell module is User Module path i.e “C:\Users\ChenV\Documents\WindowsPowerShell\Modules\SPOnlineModule” in which SPOnlineModule is the folder which contains .PSM1, .PSD1 file and required DLL’s. Ensure the psm1 and psd1 files are also named as SPOnlineModule.

Specific User

This is the location for installing modules for specific user “$home\Documents\WindowsPowerShell\Modules\

For All Users

This is the location for installing modules for all users “$EnvProgramFiles\WindowsPowerShell\Modules\”

Creating Modules

It’s very simple to create module but before distributing it ensure you have done the below

  • Test the Module
  • Include PowerShell Version
  • Create a Proper psd1 file
  • Include all required assemblies (In our case it’s required)
  • Include Help Files (This blog post will cover this!)
  • Create a Custom View for the Output

PSD1 File

This is psd1 file for the two functions we created named “Connect-xSPOTenant” and Get-xSPOList. I have moved all the SharePoint Online CSOM SDK Assemblies to my module folder. Just to distribute it to my clients and they don’t need to install the package. I have included the required dll in NestedModule Section.

PSM1 File

We haven’t added help / comments or display view for the functions. All we did is created the module with required files. The below code is my PowerShell .psm1 file which has only two functions as I mentioned earlier.

Take look at the folder contents inside my module folder – And remember we named it as SPOnlineModule
2016-05-24_16-28-30 It’s good to start! Let’s try to import the module and test the functionality so we can add the requirement components which makes our module rich and easy to use.

Fantastic, we can see the module loading successfully.
2016-05-24_16-32-01 always use Verbose switch to get information about the cmdlets we are loading to the session. Now, let’s see the available cmdlet by simply executing the below snippet

and the output is shown below
2016-05-24_16-34-35 let’s skip the functionality test because we did it in our previous blog (Part 1). So we will focus on help and custom view for output formatting in this blog post. If we run the below PowerShell code we get very basic information about the cmdlet.

Something like this
2016-05-24_16-38-45 Okay enough of theory, we just need to add comment based help for our function and PowerShell will take care of the rest. To do this in our psm1 file we will include the comment based help inside the PowerShell functions like shown below

Just by doing this we are adding more feature to our PowerShell module and makes others to read the help before using the module. Avoids many hindrances and break dependencies at work place. Any PowerShell folks can go through the code and understand it. So, now Connect-xSPOTenant looks like below

which now allows us to use help at ease.
2016-05-24_16-53-24 and examples like 34334

Part 1 – Getting Started With CSOM | SharePoint Online | PowerShell


CSOM is no more a secret for SharePoint IT Professionals and developers. MS released a new version of SharePoint Online CSOM and it’s available in Nuget which makes our life bit easier. All we need is to run Install-Package “Microsoft.SharePointOnline.CSOM” -Verbose 2016-05-24_14-32-44 We can use VS Community Edition for building binary modules or use Visual Studio Code editor to build scripts or script modules. After installing the packages I found my Nuget folder under the path “C:\Program Files\NuGet\” and the required DLL are located in “C:\Programfiles\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5026.1200\lib\net45“. The DLL files are listed below. This is a part 1 so we will just use Two DLL’s for now to get very basic information.

  • Microsoft.Office.Client.Policy.dll
  • Microsoft.Office.Client.TranslationServices.dll
  • Microsoft.Office.SharePoint.Tools.dll
  • Microsoft.Online.SharePoint.Client.Tenant.dll
  • Microsoft.ProjectServer.Client.dll
  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.DocumentManagement.dll
  • Microsoft.SharePoint.Client.Publishing.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  • Microsoft.SharePoint.Client.Runtime.Windows.dll
  • Microsoft.SharePoint.Client.Search.Applications.dll
  • Microsoft.SharePoint.Client.Search.dll
  • Microsoft.SharePoint.Client.Taxonomy.dll
  • Microsoft.SharePoint.Client.UserProfiles.dll
  • Microsoft.SharePoint.Client.WorkflowServices.dll

Using Import-Module “Path to DLL” I loaded the binaries to my PowerShell session so the Visual Studio Code will help us in intellisense. Thus, we do code at ease! The below function will establish the connection to SharePoint Online at tenant level. To understand the SharePoint Online site hierarchy refer the below image (Just as a References)a30d1b67-e6ff-4fea-bc44-5f00ba7fcc33 PowerShell Code to Connect With SharePoint Online Tenant

Just halt here! We didn’t use the conditional scopes to validate. If you pass incorrect values to variables no exception will be thrown. If you are planning to commit this in Binary module then refer this blog post. After establishing the connection we can query different site collections to get list information. For example, refer the following function which connects to one of the Site Collections and retrieve List Information.

The below figure illustrates the output.
2016-05-24_15-39-18 In our next blog we will add some more functions and show case creating modules.

SharePoint Online PowerShell Tip: Customize Your Script to Meet Client Requirements



Recently I was building a PowerShell Module for one of our customers SharePoint farm. Of course, it’s completely customized. The SharePoint Online farm has multiple site collections with different languages. Any organization located in across geographical area needs this set up. So, in one our cmdlet we retrieve site information including language (In My Lab all the site are in en-US but the code works for other LocaleID ).

Get Started

Though, I was in middle of building binary module, I just paused it for a while and used SharePoint Online Management module for testing.
1Good let’s import the module to meet our need

2 Well, module is imported with warning due to unapproved verbs and that’s not going to impact. Now, let’s connect to SharePoint Tenant by using the below cmdlet

and by using the Cmdlet Get-SPOSite we can get the information we required like shown below

3 I just selected two properties to avoid exposing domain information. 1033 is LCID of en-US (English United States). Let’s play with our native Windows PowerShell

which accepts three overloads let’s grab the first one to show the display name of the LocaleID (A property (output) of Get-SPOSite)

5 Fine, we know the way to do! Let’s include in our Get-SPOSite cmdlet to get desired output.

6 or we can get display name by executing the below code

7 Clear, in the binary module I added the below piece of code to get desired output for our content managers

and the sample code is below

Note: I have included help and format files in this code!

VS Binary Module Tip: PowerShell 5.0 SDK Reference Assemblies – Nuget

PowerShell 5.0 SDK Reference Assemblies is available in NuGet. If you are building binary modules you can add the references from Visual Studio just by executing the below command in NPM Console

In the NPM UI we can get it in one go – By Clicking Install
Cool, its easy to add the reference to our project and start building the binary module easily. When we build cmdlet we need to run and test the functionality and each time we execute it’s necessary to close the PowerShell Console Host or ISE host if the binary module is loaded and we can’t rebuild the solution. It will end up in Dll in use error. We can easily over come this by following the below steps.

  • Right Click the Solution from Solution Explorer.
  • Choose Properties and in the Right Pane Select Debug
  • Click Select External Program. Navigate to “C:\Windows\System32\WindowsPowerShell\v1.0\”.
  • Select PowerShell.exe
  • Select the option Enable native code debugging

So after building the code simple click “Start” in Visual Studio which open the PowerShell Console.
Additionally, we can load our module with the help of Command line arguments.

Enjoy PowerShell 🙂 🙂 🙂

PowerShell Tip: Extract Images from DOCX File


It’s an immense pleasure to explore $objects! We do IT Automation tasks using C# or PowerShell. It depends on the requirement and business needs. Recently our team was engaged to extract images from the DOCX files. It’s possible by simply renaming it to ZIP and extract to get the images folder which is not optimal solution at our client environment. So, we used OPEN XML SDK 2.5 (Supported for Office 2016) to accomplish the task.


Extract all the images from the given DOCX File. Example Get-Image -FilePath “C:\Temp\Document.Docx”


  • OPEN XML SDK 2.5
  • Windows PowerShell
  • Visual Studio 2015 (C# Class Library – Binary Module)
  • PowerShell Code

    Cool, here is the C# Binary cmdlet code to do the same


    We prefer to pick a tool which costs less to deliver the solution. So, at my client place we opted for PowerShell to achieve few automation task with respect to Data Manipulation. To loop through multiple files just used the below snippet:

    Enjoy PowerShell!

    Note: You can modify the filename of the image as required. In our case it needs to be the same because its customized.

    Tip: Create New List in SharePoint Online Using Exception Handling Scope and Set Properties | PowerShell | CSOM


    While presenting about SharePoint Online Client Side Object Model a question popped up “Hey! How can I create a new list if it’s not exist and change properties if exists?”. Not a difficult one. However, we need to build codes with best practise suggested by Microsoft. In short answer is available here.


    • Create a List if it’s not existing.
    • Change Properties if it exists.
    • Allow to create multiple lists in one run
    • Allow users to choose the list template.


    Build a binary cmdlet using C# and meet your needs by parameterizing the code. Look at the code below.

    How to use it?

    • Create a C# Class Library
    • Copy and paste the code. If required change the parameters as required.
    • Build it to get the DLL (Binary)
    • Using Import-Module load the binary DLL E.G. Import-Module C:\Location\Solution.DLL
    • Run the cmdlet New-SPOList -SPOUrl -SPOListName -SPOListTemplateType -SPOCredential

    Here the SPOListTemplate populate the enum values of List Template – So, we can choose the one we need by tab completion

    How it works?

    We have used Exception handling scope which makes one call to the server and that means a lot of performance improvement. All three Try, catch and finally executes at one time.
    This code simply checks the existence of the list and if exists it will update the hidden and description properties.
    If try catch throws exception it will create a new list – If in case of the list not existing.
    Update the lists properties.

    Screen Shot


    SharePoint Online PowerShell Tip: Conditional Scope – Binary Module

    SharePoint Online PowerShell Tip to throw exception while connecting to Tenant.


    Recently one of our customer requested us to share a code which should throw an exception while connecting to SharePoint Online if in case of credential invalid or not connecting to tenant site. Well, they have a binary module delivered by some suppliers which is pretty straight forward and simple. If all set to good code will execute if not exception thrown in other cmdlet. The module they use has bunch of cmdlets which are more or less mock up of SharePoint Online cmdlets. Connect-SPOService throws exception if credential are invalid and they need custom error message for the custom built cmdlet Connect-SPOTenant


    • Connect-SPOTenant : The sign-in name or password does not match one in the Microsoft account system
    • Connect-SPOTenant : Current site is not a tenant administration site.

    PowerShell Code

    Below is the PowerShell code shared by our client.


    Indeed, the above code will not throw any exception because the ClientContext Object is instantiated and will not validate the credential but returns the ClientContext Object as output. More over Microsoft.SharePoint.Client.ClientContext doesn’t have ServerObjectIsNull property. No matter we supply wrong password the host shows Connected to Site!

    Conditional Scope

    C# code with conditional scope is below. This is just a demo so we haven’t used any scope methods or properties

    Screen Shot



    Bulk Import Folders and Files in SharePoint Document Library | Part 1

    This noon I was on a call with SharePoint Development team to discuss few activities which is pretty straight forward approach for SharePoint IT Professionals. However, we need to inform about the activity we carry out in the farm to both IT Pro’s and developers. By doing this we simply avoid confusion. And, as always in any farm bulk upload of files and folders may cause one or other issues like threshold, performance high risk of uploading vulnerable files etc.

    Our client came with 5000 odd folders which has almost 9000 files approximately and the requirement is to push it to document folder. For example, files underneath the folder should be organized as a single folder and files needs to be stored in it. The appended a request “Share the cmdlet so we can add more in future!”

    Everything is easy if we build one! There is no out of the box solution. We can’t drag and drop easily as we know the limitations in document library. So, I built a small cmdlet in Visual C# which serves the purpose.

    The folder structure is very simple, Main Folder which contains N number of folder and each folder may have some files. No Folders underneath the folders.


    In Part 2 we will discuss about the best practice and will enhance this code much better! Enjoy PowerShell 🙂 🙂 🙂

    Thanks to Author Rashu, Rahul in TechNet Wiki