Ultimate Guide: Mastering Month-End Closing With Microsoft Dynamics NAV 2017

Download

Introduced in 2007, PowerShell is a powerful task automation and configuration management framework that provides command-line interface for Windows and Linux administrators. Up your PowerShell game with these 6 commands for Dynamics 365 customization.

Powershell and Dynamics 365

Windows PowerShell is a task-based command line shell and scripting language that enables users to control and automate the administration, deployment, and management of applications running on Windows and Linux. More specifically, it uses cmdlets (PowerShell’s version of commands) and the .NET framework to perform system administration tasks.

In this blog I’ll explain why PowerShell and Dynamics 365 (Dynamics CRM) make such a good team and share 6 of my favorite commands. 

Why should I use PowerShell and Dynamics 365?

In my opinion, PowerShell has three main benefits: discoverability, repeatability, consistency.

Discoverability

Firstly, PowerShell is easy to use and navigate. When Windows was first introduced to the public all applications followed a common design language (icons were always positioned along the top of the application, which could be used to minimize and maximize the application). These discoverability traits are mirrored in PowerShell. It follows a command discoverability framework and it’s “Get-Help” command is the starting point for anyone interested in starting to use PowerShell or just needing a helping hand.

One of the biggest discoverability aspects of PowerShell is tab completion. With tab completion a user can start typing the first few characters of the command and PowerShell will complete it. Not only does this speed up their ability to interact with PowerShell, but it also enables them to cycle through available commands easily.


Pro Tip: To use tab completion simply start a command like “Get-H” and select the TAB key on your keyboard to complete the command to “Get-Help.” If you want to see more results that begin with H continue tapping the TAB key to cycle through all the results.


In the past, IT departments/consultants would create step-by-step documentation detailing where to click or what commands to type into a window. Whether the script worked was completely dependent on who was executing it within the operations team. However, it is now possible for IT departments to author scripts and have them executed by the operations people. In other words, they can be run within environments such as Dev, Test or Production without worry that a step will be missed.

Repeatability and consistency

In terms of repeatability and consistency, PowerShell has made it possible to create repeatability while executing the same task. For example, in my position as an Infrastructure Consultant I do a lot of deployments of Dynamics CRM. With PowerShell, I’m able to use a scripted approach to installing software up to a certain point, ensuring that the deployment matches what is written in the script. This approach is referred to as “declarative programming”. The script you use for your deployment now becomes your installation documentation, which can be managed by adding the script to source control (Visual Studio Team Services, Github, BitBucket, etc).


Like What You've Read? Subscribe to Our Newsletter!

What kinds of things can I do with PowerShell and Dynamics 365?

With PowerShell you can do anything and everything. I use PowerShell for things as common as verifying that the correct CRM license has been installed in a Dynamics CRM deployment, importing new CRM solutions and adding security roles to users. I also use it for more complex tasks such as creating new CRM records automating the entire installation and configuration of Dynamics CRM on premise.

It’s worth noting that PowerShell can just as easily be used in a personal capacity. For example, you can create to-do lists, upload photos, videos and documents to SharePoint and much more. I often use PowerShell to build out task lists for the day’s to-do’s.

How do I get started with PowerShell and Dynamics 365?

First you’ll need to obtain the PowerShell module for Dynamics 365. If you’re using Windows 10 you can install this by running the following command from an Administrative PowerShell session install-module Microsoft.Xrm.Data.Powershell.

If you’re using an older version of Windows you’ll need to download the module and ensure its added to your PowerShell module directory. You can find more details here.


Like What You've Read? Subscribe to Our Newsletter!

6 commands to take you from zero to PowerShell Hero

This will help you gain a better understanding of how a PowerShell command should be executed, what parameters it accepts and what kind of output you can expect.

This requires that you provide your credentials to Dynamics 365 Online, but you will be automatically connected to your 365 Organization after the command completes. Take this command a step further and save your connection to a variable that you can use later with:

$connection = Connect-CRMOnlineDiscovery -InteractiveMode

 

PowerShell and Dynamics 365 - PowerShell Interactive Login 

While similar to the previous command, this used strictly when interacting with Dynamics 365 that is within your own premise. You’ll need to provide some additional parameters to ensure you make a successful connection, such as your credentials to connect and the server URL. Here is an example:

Connect-CrmOnPremDiscovery -Credential (Get-Credential) -ServerUrl http://crm2016:5555

Again, you can save your connection into a variable that you can use later simply by adding it in front of your Connect-CrmOnPremDiscovery. Here is an example:

$connection = Connect-CrmOnPremDiscovery -Credential (Get-Credential) -ServerUrl http://crm2016:5555

PowerShell and Dynamics 365 - Get Credential On Prem Discovery

When I’m heads down performing Dynamics 365 customizations, it can be a time sink to always publish my updated customizations from within the web interface. I much prefer using the Publish-CRMCustomization approach to ensure my changes are published and ready for me to review.

Here is an example of how to use it, and accepts the connection to CRM variable we created above:

Publish-CRMCustomization -conn $connection -Entity -EntitiyLogicalNames account,contact,systemuser
PowerShell and Dynamics 365 - Publish Customization

This command is very similar to the one above, with the exception that it publishes all 365 customizations instead of only specific entities or items. You can use it just like the command previously mentioned:

Publish-CrmAllCustomization -conn $connection -Verbose

PowerShell and Dynamics 365 - Publish All Customization

This is one of my personal favorites to run. It allows me to quickly benchmark a Dynamics 365 view and validate that the changes I made to the view aren’t causing a performance hit to users. As well, I’ll often use it in troubleshooting scenarios when a customer has explained that certain views take a long time to load. After tweaking the columns in the view I can run this command to verify if my changes to the view were successful in reducing the time required to load it. Can you locate the view’s you would like to test by either using the View GUID value or the logical name of the system view, in this example I tested the performance of the “Active Accounts” view.

Test-CrmViewPerformance -conn $connection -ViewName "Active Accounts"

PowerShell and Dynamics 365 - Test CRM Performance

Questions or comments about this PowerShell and Dynamics 365 blog?

For those of you new to PowerShell, I hope this blog has opened some doors for you. It’s a great tool to have in your back pocket and I highly recommend diving in! You can find a list of  Windows PowerShell cmdlets for Microsoft Dynamics 365 here. Those of you already using PowerShell and Dynamics 365,  I’d love to hear what your favorite command is!

Please don’t hesitate to leave a comment below or reach out to me here.

 

Comments

  1. Tim Koehler

    Hello Dan,
    thanks for your post. I just entered the field because of my new employment.
    How do you list all users (or accounts) in a readable/ usable way?

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

    shows all users in one value string, but I dont know how to select one or more for later use; for example changing settings.

    Can you please publish more examples about your common tasks so I can have a better idea about how to use the xrm PS module.

    Thanks in advance.
    Best regards,
    Tim

    1. Hi Tim,

      Thanks for your question – and welcome to the Microsoft / PowerShell universe!

      There’s lots that you can do with PowerShell. Can you suggest a few tasks that you’d like to see as examples and then I can go from there? It’ll help me narrow the field.

      Thanks!
      Dan

  2. Osayawe

    I have been looking for something like this. I got a flat “Not Possible” to use powershell to assign security roles in Dynamics 365.
    Would you share some commands on assigning security roles to Dunamics 365?

    1. Hi Osayawe,

      Thanks for the comment. I highly recommend that you check out the XrmToolBox plugin’s “User Roles Manager” as I think it will help you achieve the same thing. You can find it here:

      https://community.dynamics.com/CRM/B/DYNAMICSCRMTOOLS/ARCHIVE/2014/07/16/NEW-XRMTOOLBOX-PLUGIN-USER-ROLES-MANAGER

      Hope that helps & good luck!

      Dan

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Filter:

Archives

Register to recieve the latest ERP & CRM insights