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
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.
Once the window loads, type the below line
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
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.
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
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
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
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
The next step is to connect to the organization using the above credentials. And this what I do.
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.
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
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.
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.
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.
Yes you can query records as well just like you do in query expressions. However you are limited as power of Query Expression goes.
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.
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.
Easy isn’t it.
How many click when you export the solution? No need to open CRM even. Here is the one liner code for you.
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.