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

Install-Package Microsoft.PowerShell.5.ReferenceAssemblies -Verbose

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.

-NoExit -Command "Import-Module .\PowerShellDemo.dll"

Enjoy PowerShell 🙂 🙂 🙂

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.

function global:Connect-SPOSite {
 param (
 [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
 begin {
 [System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll") | Out-Null
 [System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll") | Out-Null
 process {
 if ($global:spoCred -eq $null) {
 $cred = Get-Credential -Message "Enter your credentials for SharePoint Online:"
 $global:spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
 $ctx = New-Object Microsoft.SharePoint.Client.ClientContext $Url
 $ctx.Credentials = $spoCred
 if (!$ctx.ServerObjectIsNull.Value) {
 Write-Host "Connected to site: '$Url'" -ForegroundColor Green
 return $ctx
 end {


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

using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;
namespace xSharePointOnline
 [Cmdlet(VerbsCommunications.Connect, "SPOTenant")]
 public class ConnectSPOTenant : PSCmdlet
 public Uri SPOUrl;
 public PSCredential SPOCredential;
 protected override void ProcessRecord()
 using (ClientContext SPOClientContext = new ClientContext(SPOUrl))
 SPOClientContext.Credentials = new SharePointOnlineCredentials(SPOCredential.UserName, SPOCredential.Password);
 Tenant oTenant = new Tenant(SPOClientContext);
 ConditionalScope Scope = new ConditionalScope(SPOClientContext, () => oTenant.ServerObjectIsNull.Value != true);

Screen Shot



Show or Hide SharePoint List Using CSOM | C# | PowerShell

Disclaimer: This is pretty old topic just remade it using CSOM and PowerShell at my client work place.

During SharePoint due diligence many suggested to use SharePoint Manager Tools, CAML Query Builder etc. It’s better to keep tools handy but gathering and executing the client requirement is better than the best.

Environment: Client has WSS 3.0, MOSS 2007 and SharePoint 2010 platforms, they want to organize the structure and keep the contents similar. An ideal goal is to move to upgraded versions.

During this big discussion development team suggested to make a script which reads the web, site, lists and document library properties and set the same in each farm. Dude! writing script is easy and deploying is very easy as well. But, before we do that let’s know why properties are set differently ? Who made the decision? Business Justification is much more important so I suggested Power Users or SharePoint L1 to decide and deploy the solution.

How? Power Users like GUI based solutions they don’t want to execute console applications or scripts. As usual we picked up a call and I was shocked by listening to content manager requirement – “Need one liner cmdlet and we will create Lists and Set it’s Property. It may be one or many!” It’s that simple PowerShell reached to content managers as well. 🙂 I was happy because I am going to meet their needs in short span of time!

To kick on the demo in a catchy way. I showed them how PowerShell one liner looks like 🙂

Get-EvenNumbers -From 1 -To 10

It’s that simple to use, I haven’ t used any validation in this function very simple code which prints the even number using range operators. The function code is below

Function Get-EvenNumbers
    [Parameter(Mandatory = $true)]

    ($From..$To).ForEach({if($_ %2 -eq 0){$_}})
Get-EvenNumbers -From 1 -To 10

Solution for Content Managers:

Consume CSOM and build binary module using Class Library 🙂 🙂 🙂 This time I used Visual Studio 2013 just for a change 🙂

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Net;
using System.Management.Automation;
using Microsoft.SharePoint.Client;

public class SetSPListVisibility : Cmdlet
    [Parameter(Mandatory = true)]
    public string SPUrl;

    [Parameter(Mandatory = true)]
    public string SPListName;

    [Parameter(Mandatory = true)]
    public bool ListVisibility;
    protected override void ProcessRecord()
        ClientContext ctx = new ClientContext(SPUrl);
        NetworkCredential credentials = CredentialCache.DefaultNetworkCredentials;
        ctx.Credentials = credentials;

        List list = ctx.Web.Lists.GetByTitle(SPListName);

        list.Hidden = ListVisibility;

Usage (Just for Demo):

Import-Module C:\Scripts\Module\CSOM_Demo.Dll
#Hide the List
Set-SPListVisibility -SPUrl http://weburl -SPListName "PowerShell" -ListVisibility $false

#Show the List
Set-SPListVisibility -SPUrl http://weburl -SPListName "PowerShell" -ListVisibility $true

In next my next blog I will share the complete module! It’s that’s easy ! Happy SharePointing and Enjoy PowerShell 🙂 🙂 🙂

Reason: Content manager need this kind of solution for re usability. Smart Guy!