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.
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 six of my favourite commands.
Why should I use PowerShell and Dynamics 365?
In my opinion, PowerShell has three main benefits: discoverability, repeatability, consistency.
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).
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.
6 commands to take you from zero to PowerShell Hero
[toggle title=”1. Get-Help -examples” class=”in”]
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.
[toggle title=”2. Connect-CrmOnlineDiscovery -InteractiveMode “]
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
[toggle title=”3. Connect-CRMOnPremDiscovery“]
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 https://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 https://crm2016:5555
[toggle title=”4. Publish-CRMCustomization“]
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
[toggle title=”5. Publish-CRMAllCustomization“]
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
[toggle title=”6. Test-CrmViewPerformance“]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”
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!