Executing the Azure Active Directory Module (MSOL Commands) Power Shell commands from the C#

Executing the Azure Active Directory Power Shell commands from the C#.

What is a use of the Azure PowerShell Module?

Azure Active Directory Module (ADPS) for Windows PowerShell cmdlets for Azure AD administrative tasks such as user management, domain management and for configuring single sign-on.

Executing the Azure PowerShell from the C# code:

Most of the Azure administrative work performed through the PowerShell jobs only and some scenarios we have to execute PowerShell commands in C#.
            I am going to explain how to execute Azure PowerShell from the C#.
To start writing the code the first step is we need to install Azure PowerShell Module as mentioned below.

Install the Azure AD Module

The Azure AD Module is supported on the following Windows operating systems with the default version of Microsoft .NET Framework and Windows PowerShell: Windows 8.1, Windows 8, Windows 7, Windows Server 2012 R2, Windows Server 2012, or Windows Server 2008 R2.
First install the Microsoft Online Services Sign-In Assistant for IT Professionals RTW from the Microsoft Download Center. Then install the Azure Active Directory Module for Windows PowerShell (64-bit version), and click Run to run the installer package.

Scenario: Get the Security Group Name from Azure Active Directory by key word.

Code for the Console Application.

                                     Download Code 
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)


        public static void GetSecurityGroup()
                // Create Initial Session State for runspace.
                InitialSessionState initialSession = InitialSessionState.CreateDefault();
                initialSession.ImportPSModule(new[] { "MSOnline" });
                // Assign username to variable.
                UserCredential.UserName = "UserName"; // Pass Your AD User Name

                string password = "Your Password"; //  Pass your Password
                // Create and assign secure password string.
                SecureString securePass = new SecureString();
                foreach (char secureChar in password)

                UserCredential.Password = securePass;
                // Create credential object.
                PSCredential credential = new PSCredential(UserCredential.UserName, UserCredential.Password);
                // Create command to connect office 365.
                Command connectCommand = new Command("Connect-MsolService");
                connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
                // Create command to get office 365 user.
                Command getGroupCommand = new Command("Get-MsolGroup");
                getGroupCommand.Parameters.Add((new CommandParameter("SearchString", "Pass your security group name")));
                using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
                    // Open runspace.
                    //Iterate through each command and executes it.
                    foreach (var com in new Command[] { connectCommand, getGroupCommand})
                        var pipe = psRunSpace.CreatePipeline();
                        // Execute command and generate results and errors (if any).
                        Collection<PSObject> results = pipe.Invoke();
                        var error = pipe.Error.ReadToEnd();

                        if (error.Count > 0)
                         Microsoft.Online.Administration.Group Group = (Microsoft.Online.Administration.Group)results[0].BaseObject;
                                  string  Groupid = Group.ObjectId.ToString();
                                  string GroupName = Group.DisplayName;

                    // Close the runspace.
                //Cursor.Current = Cursors.Default;
            catch (Exception)


  1. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy.

    I will bookmark your site and check again here often. I’m quite sure :)

    ERP Software Dubai

  2. ERPTREE is a leading oracle fuison HCM Training Institute. we offer this course through online we have great
    experience in succeeding students through online courses. we can calculate our performance through their honest
    comments in our sites in supporting our services. we have referral program so candidates can earn money through
    referral. you can share your live experience with other can generate you some money.

    Oracle fusion HCM Online Training

    Oracle Fusion HCM Training

  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
    Android training in chennai
    Ios training in chennai

  4. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Microsoft azure, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Microsoft azure. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.

    For Free Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com

  5. Thanks for your valuable info and time, nice and very useful article, thanks for sharing your knowledge.
    Oracle Training in chennai

  6. Hello,

    Do we need to add any reference to use the "Get-MsolPartnerContract" command in our c# code are any 'MSol" command

    Can you please help me here i am stuck

  7. product and to understand cloud storage better, one needs to undergo a Microsoft Azure fundamentals training course. Azure fundamental course

  8. Thanks for sharing this great information on Oracle Fusion HCM. Actually I was looking for the same information on internet for Oracle Fusion HCM Interview Questions and Answers and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject, you can learn more about Fusion financial by attending Oracle Fusion HCM Training.

  9. online professional listings can be an extraordinary expansion to your present private company advertising procedure. local personal classifieds


Popular Posts