Great Power Tools for CRM Administrator: Microsoft.Xrm.Data.PowerShell module – Part II


During our Part I of this article, we went through step-by-step process of setting up the Microsoft.Xrm.Data.PowerShell module. I am sure you are ready to see it in action. During this part, we are going to create a sample account, update the record, then remove the account. We are also going to look at how to make user settings change via PowerShell. So let's get started.

Tryout with a Sample Account

If you have successfully completed all previous steps, congratulations. Now you can create an account and store record Guid to a variable by running the following command:

$accountId = New-CrmRecord -conn $conn -EntityLogicalName account -Fields @{"name"="Sample Account";"telephone1"="555-5555"}

You can go to your Dynamics CRM environment to verify the creation of this account.

 

 

Next, you can run the following PowerShell command to display the Guid for this account:

$accountid

Below is a sample output.

 

The following command helps you to retrieve a record and store record to a variable:

$account = Get-CrmRecord -conn $conn -EntityLogicalName account -Id $accountId -Fields name,telephone1

Now, you can display the record:

$account

If you are wondering how to update a record, here is an example of setting new name value for the sample account:

$account.name = "Sample Account Updated"

Set-CrmRecord -conn $conn -CrmRecord $account 

Back to your CRM environment, you should see your sample account has been updated.

You can run the following command to retrieve the record again and display the result:

Get-CrmRecord -conn $conn -EntityLogicalName account -Id $accountId -Fields name

Below is a sample output:

 

 

Finally, we can do some cleanup work by remove this sample account we just created:

Remove-CrmRecord -conn $conn -CrmRecord $account

If you were on that sample record in your CRM environment, by now, you can refresh your record and it will show you the record is no longer available. If you look at the list of accounts, you should no longer see the sample account record.

 

Retrieve CRM Users

In this example, we are going to get a list of CRM users in your organization. Assume you have successfully connected to the CRM environment from the previous example, you can run the following PowerShell command:

$users = Get-CrmRecords -EntityLogicalName systemuser -FilterAttribute isdisabled -FilterOperator eq -FilterValue $false -Fields systemuserid,fullname

This should get a list of users. Then run the following command to get the exact total number of users:

$users.Count

Also, you can run the following command to see user details:

$users.CrmRecords

You should see an output similar to the screen output below:

 

Change User Settings

You can change user settings using PowerShell commands. In this section, we will change a time zone setting for one of our CRM users. This users used be working on east coast, now she is moved to the west coast. So we need to change her user setting from east coast time zone to west coast time zone.

If you like to see more examples, please download the sample file and make sure you read the ReadMe document first. The sample file download can be found at Microsoft.Xrm.Data.Powershell: PowerShell for Dynamics CRM Organization Data.

Let’s get CRM time zone information so we know the time zone code for east and west coast by using the following command:

Get-CrmTimeZones

From the previous example, you have successfully retrieved a list of users with their corresponding ids. So now we can update this user’s time zone setting. First, let’s retrieve her current time zone setting by using the following command with this user’s system id:

$userSettings = Get-CrmUserSettings -UserId e5abdcfc-6967-e311-9408-00155d11652e -Fields timezonecode

We see that this user’s time zone is currently set to Eastern Time with value of 35.

To change this user’s time zone setting to Pacific Time, you can use the following command:

$userSettings.timezonecode = 4
Set-CrmUserSettings -conn $conn -CrmRecord $userSettings

The sample PowerShell script and output looks similar to this:

 

 

Understand Command Details

From your previous exercise, you have successfully created, updated, and deleted a sample account. Also, you have retrieved a list of CRM users and updated a user setting. There are so much more you can do with this PowerShell module. You can run following command to get all commands:

Get-Command *crm*

Also, you can run the following command to get help when you need it:

Get-Help New-CrmRecord -Detailed

Below is a sample output for some commands:

 

 

Final Thoughts

The Microsoft.Xrm.Data.PowerShell CRM PowerShell module is an awesome tool for CRM Administrators and everybody likes to use PowerShell. As you have seen in our example, it can perform CRUD operations with Dynamics CRM data and change user and system Settings.  Best of all, it works for both Dynamics CRM Online and On-Premise environments. So if you haven’t use it, I recommend—

  1. Download the Microsoft.Xrm.Data.PowerShell CRM PowerShell module
  2. Try out your scenarios
  3. Provide feedback or ask questions on the website--Microsoft.Xrm.Data.Powershell: PowerShell for Dynamics CRM Organization Data

 

Related References

Microsoft.Xrm.Data.Powershell: PowerShell for Dynamics CRM Organization Data

Manage Dynamics CRM data and settings with PowerShell! 

Using Windows PowerShell 

 

Thanks,

Andy Zhang

Microsoft Premier Field Engineer

Comments (0)

Skip to main content