---
title: "Use PowerShell to communicate with Dynamics CRM using the .NET XRM SDK"
description: "Another way to interact with the CRM DLL's is through PowerShell. PowerShell is built upon .NET meaning you can call exactly the same CRM libraries from PowerShell as from .NET applications."
canonical: "https://swimburger.net/blog/dynamics/use-powershell-to-communicate-with-dynamics-crm-using-the-dotnet-xrm-sdk"
published: "2020-04-20T00:00:00.000Z"
updated: "2020-11-24T05:14:59.293Z"
---

# Use PowerShell to communicate with Dynamics CRM using the .NET XRM SDK

If you have developed client applications or plugins for Dynamics CRM, you likely are familiar with the CRM/XRM libraries. You may have gotten those DLL's from the CRM installation, the SDK zip, or the [**NuGet package**](https://www.nuget.org/packages/Microsoft.CrmSdk.XrmTooling.CoreAssembly/ "NuGet package 'Microsoft.CrmSdk.XrmTooling.CoreAssembly'"). Another way to interact with these CRM DLL's is through PowerShell. PowerShell is built upon .NET meaning you can call the same CRM libraries from PowerShell as from .NET applications.

The easiest way to start using PowerShell with CRM libraries, is to install the "[Microsoft.Xrm.Tooling.CrmConnector.PowerShell](https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell "PowerShell module \"Microsoft.Xrm.Tooling.CrmConnector.PowerShell\"")" PowerShell module. Note all the DLL's that come with this PowerShell module which you can find on the [NuGet page](https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell "PowerShell module \"Microsoft.Xrm.Tooling.CrmConnector.PowerShell\"") under 'FileList':

![PowerShell Module FileList containing many CRM DLL's](/media/42vhbnvg/image.png)

This module contains many DLL's but most importantly, the core CRM DLL's you usually use such as:

-   Microsoft.Xrm.Sdk.Deployment.dll
-   Microsoft.Xrm.Sdk.dll
-   Microsoft.Xrm.Sdk.Workflow.dll
-   Microsoft.Xrm.Tooling.Connector.dll

Use the following command to install the PowerShell module:

```powershell
Install-Module -Name Microsoft.Xrm.Tooling.CrmConnector.PowerShell
```

The PowerShell module only provides two PowerShell functions:

-   [Get-CrmConnection](https://docs.microsoft.com/en-us/powershell/module/microsoft.xrm.tooling.crmconnector.powershell/Get-CrmConnection?view=pa-ps-latest): This function returns the trusty "[CrmServiceClient](https://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.tooling.connector.crmserviceclient?view=dynamics-xrmtooling-ce-9 "Documentation for CrmServiceClient Class")"
-   [Get-CrmOrganizations](https://docs.microsoft.com/en-us/powershell/module/microsoft.xrm.tooling.crmconnector.powershell/Get-CrmOrganizations?view=pa-ps-latest): Returns the CRM Organizations

Although the module only provides these two PowerShell functions, the entire XRM SDK is made available to you.

For example, here's a PowerShell script file named "[GetOptionSet.ps1](https://gist.github.com/Swimburger/4b1a7583be2ba286ebf404e083287893)" that will print out all optionset values and labels for a given Entity + OptionSet-Attribute:

```powershell
Param(
    [Parameter(Mandatory=$true)]
    [String]
    $ConnectionString,
    [Parameter(Mandatory=$true)]
    [String]
    $EntityLogicalName,
    [Parameter(Mandatory=$true)]
    [String]
    $OptionSetAttributeName
)

# Import the Dynamics XRM PowerShell module
# https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell/3.3.0.887
Import-Module Microsoft.Xrm.Tooling.CrmConnector.PowerShell;
# Get a CrmServiceClient to communicate with Dynamics CRM
$CrmClient = Get-CrmConnection -ConnectionString $ConnectionString;

# Create a RetrieveAttributeRequest to fetch Attribute metadata
$AttributeRequest = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeRequest]::new();
$AttributeRequest.EntityLogicalName = $EntityLogicalName;
$AttributeRequest.LogicalName = $OptionSetAttributeName;
$AttributeRequest.RetrieveAsIfPublished = $True;
$AttributeResponse = [Microsoft.Xrm.Sdk.Messages.RetrieveAttributeResponse]$CrmClient.Execute($AttributeRequest);

# Get the Value/Label pairs and print them to the console
$AttributeResponse.AttributeMetadata.OptionSet.Options `
    | Select-Object -Property `
        @{Name = "Value"; Expression={$_.Value}},`
        @{Name = "Label"; Expression={$_.Label.UserLocalizedLabel.Label}};

# Close the connection to CRM
$CrmClient.Dispose();
```

To use the script, open a PowerShell window and invoke the PowerShell file as below:

```powershell
# Follow the link below to learn how to create your connectionstring:
# https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/xrm-tooling/use-connection-strings-xrm-tooling-connect#connection-string-parameters
$CrmConnectionString = "YOURCONNECTIONSTRING";

.\GetOptionSet.ps1 `
    -ConnectionString $CrmConnectionString `
    -EntityLogicalName "lead" `
    -OptionSetAttributeName "statuscode";

# Output will look something like this:
#   Value Label
#   ----- -----
#       1 New
#       2 Contacted
#       3 Qualified
#       6 Not Interested
#       5 Unable to Contact
```

Using PowerShell, you can provide a lot of value with a single file as opposed to .NET projects which require at least multiple files, folders & compilation. I still prefer creating .NET projects for larger endeavors, but PowerShell has been very helpful for scripting things quickly. Being able to pass single script files between machines and colleagues has also been very handy.

In the end you can achieve anything with either PowerShell or .NET projects, so whatever floats your boat!
