How to get Authentication Token for Dynamics 365 Finance and Operations on-premise and cloud deployments to run REST API using Powershell and the client credentials grant method

Before being able to run REST APIs to do specific tasks programmatically in Dynamics 365 for Finance and Operations (hereby known as D365FO), the application needs to be able to authenticate the code that it is coming from a trusted source. By far the most common deployment to date is…