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 6 of my favorite 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).
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
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
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
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
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
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!
Please don’t hesitate to leave a comment below or reach out to me here.