---
title: "PowerShell snippet: Get optionset value/labels from Dynamics CRM Entity/Attribute"
description: "Instead of having to use the CRM interface to copy all the labels and values manually, you can save yourself a lot of time using this PowerShell script."
canonical: "https://swimburger.net/blog/dynamics/powershell-snippet-get-optionset-value-labels-from-dynamics-crm-entity-attribute"
published: "2020-04-20T00:00:00.000Z"
updated: "2020-11-24T05:15:14.433Z"
---

# PowerShell snippet: Get optionset value/labels from Dynamics CRM Entity/Attribute

Instead of having to use the CRM interface to copy all the labels and values manually, you can save yourself a lot of time using this PowerShell script.  
Using the following script file named "[GetOptionSet.ps1](https://gist.github.com/Swimburger/4b1a7583be2ba286ebf404e083287893)", you can list all the value/label pairs 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,

-   install the PowerShell module "[Microsoft.Xrm.Tooling.CrmConnector.PowerShell](https://www.powershellgallery.com/packages/Microsoft.Xrm.Tooling.CrmConnector.PowerShell "PowerShell Gallery page for \"Microsoft.Xrm.Tooling.CrmConnector.PowerShell\"")",
-   open a PowerShell window,
-   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
```
