{Dynamics CRM + Powershell} Leverage Powershell to simplify your day to day CRM tasks

I have been using powershell for sometime now for my day to day job in my CRM project. And yes, whenever I use it, I simply love it. Just today another CRM consultant walked up to my desk and saw me running some CRM commands in PowerShell and he was simply amazed about quickly we can do stuffs without actually opening CRM. I showed him al the tricks and later I thought why not post it in my blog so that anybody who still does not know how to use it, can leverage the same. And yes you can do this everything from your desktop only.

So let’s start.

The first step is download the link – https://github.com/seanmcne/Microsoft.Xrm.Data.PowerShell and download the wonderful library which make all this possible. A zip file would be download Microsoft.Xrm.Data.PowerShell-master.zip. Unzip the file, you would see one folder – Microsoft.Xrm.Data.Powershell

image

 

Copy this folder and go to the location – c:\users\<username>\My Documents\

Create the folder in MyDocuments in the following hierarchy – WindowsPowerShell –> Modules and within modules paste the copied folder.

Launch Powershell ISE. You can launch just the default Powershell window but ISE will give you intellisense as well.

image

Once the window loads, type the below line

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

image

This line basically allows you to import the Microsoft.Xrm.Data.Powershell.dll which is unsigned. You would get a confirmation box once you execute the above code. Click on ‘Yes’

Once the above command is executed successfully, execute the below line.

Import-Module Microsoft.Xrm.Data.Powershell

Once done you are all set. But first lets see what we can do.

In the window type get-command *crm*  and you could see all the CRM related commands

 

image

There are 81 commands to use which will aid you greatly in your day-day tasks. I am not going to show all but I surely enough to make you curious and then out of the curiosity you would explore the rest Smile

First things first. People new to this might get overwhelmed about how to use these commands. No worries, PowerShell has your back here. To get detailed help for any of the commands just type the below line.

get-help Get-CrmOrganizations -detailed

 

image

Pretty detailed isn’t it.

Now the first thing you need to do is to connect to CRM organization and for that you would need credentials.

Type in the command window – get-credential. A window will pop-up to take the credential. In the below example I have take the credentials in a variable called $cred so that I can use it for later use.

Enter the credentials specific to the organization you want to connect to

image

 

The next step is to connect to the organization using the above credentials. And this what I do.

image

As you can see from the above screenshot, I just supplied the credentials and the Server Url. Just provide the server url not the entire organization service url. Once I press enter, system connects to CRM and determines I have access to two organizations. I select the first organization

And then my connection is set-up. Don’t worry if you are in online organization. Just like Connect-CrmOnPremDiscovery you have Connect-CrmOnlineDiscovery

Now coming to the interesting part – exploring the crm commands.

Get-CrmEntityName

What if you have the entitytypecode and need the logical name of a custom entity. No need to open CRM. Just a single line of code in the powershell

image

 

Get-CrmEntityTypeCode 

How many times we need this? I think lot of times. I have the entitylogicalname but I need the entitytypecode. No worries just one line.

image

 

G
et-CrmEntityOptionSet
  

Now here comes the big one. What if you are writing some jscript code and you have a picklist to refer to but as with everyone you cannot remember the picklist text and value (I wonder who can. Must be a genius). Just a couple of lines below and you get it. And yes intellisense is there to help you as you write this code.

image

Wonderful isn’t it. And did you observe one thing in the above code. It’s not case-sensitive as well unlike C#.

Similar to this, you have Get-CrmGlobalOptionSet  as well to explore the global option sets.

 

Get-CRMRecords

Yes you can query records as well just like you do in query expressions. However you are limited as power of Query Expression goes.

image

In the above example I just querying for all the accounts whose name begins with Test and then iterating and printing the names of the returned accounts.

 

Get-CrmRecordsCount

Say somebody would walk up to your desk and ask hey man, how many opportunities you have in your system. Say you do not have database access to run a SQL query on. And before you think of other ways, here is the code for you.

image

Easy isn’t it.

 

Export-CrmSolution

How many click when you export the solution? No need to open CRM even. Here is the one liner code for you.

image

In the above code, I am exporting a solution named ‘Sitemap’ as Managed solution and saving it in my location – c:\debajit as FromPowershell.zip.

 

Cool isn’t it. I would not show you all. By this time you have idea already. So start exploring. And yes you have 81 of the cool stuffs to explore. And all this from your desktop without even opening your CRM.

 

Hope this helps! Till you read one of my blog posts, happy CRMing.

5 thoughts on “{Dynamics CRM + Powershell} Leverage Powershell to simplify your day to day CRM tasks”

  1. Hi Debajit, this utility won’t work for me for whatever reason. I have .NET 4.6.1 and PowerShell 4 installed on my machine. I’m getting the following error message:
    Import-Module : Could not load file or assembly
    ‘file:///C:\Users\user01\Documents\WindowsPowerShell\Modules\Microsoft.Xrm.Data.Powershell\Microsoft.Crm.Sdk.Proxy.dll’ or one of its dependencies. Operation is
    not supported. (Exception from HRESULT: 0x80131515)
    At line:1 char:1
    + Import-Module Microsoft.Xrm.Data.Powershell
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Import-Module], FileLoadException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

    1. Hi Tony,
      Thanks for reading my blog.
      Could you check for the path – C:\Users\user01\Documents\WindowsPowerShell\Modules\Microsoft.Xrm.Data.Powershell\ and see if the Microsoft.Crm.Sdk.Proxy.dll is in place there. Sometime what happens is when you extract Microsoft.Xrm.Data.Powershell, it creates another folder within it with the same name and then within it the contents.
      Let me know.

        1. Hi Tony,
          Thats pretty strange. I did try at my other colleague’s laptop and it worked as well.
          As a check did you run the step to allow you to load remote signed dll using set-executioncontext as shown
          in the first few lines of the blog?

          1. Hi Debajit, yes I did run all the steps and the one that loads remote signed dll ran just fine.

Comments are closed.