Setting a Default Environment for Power Platform using PowerShell

Loading

Setting a default environment in Power Platform ensures that all PowerApps, Power Automate flows, and Dataverse components are managed within a specific environment unless explicitly changed. This is useful for governance and ensuring users work in the correct environment.


Step 1: Install the Required PowerShell Modules

Before setting a default environment, ensure you have the necessary PowerShell modules installed.

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -AllowClobber
Install-Module -Name Microsoft.PowerApps.PowerShell -Force -AllowClobber

If prompted, press Y to confirm the installation.


Step 2: Authenticate to Power Platform

To connect to Power Platform, use the following command:

Add-PowerAppsAccount

This will prompt a sign-in window where you must use your Global Admin or Power Platform Admin credentials.

For automation without manual sign-in, use service principal authentication:

$clientId = "your-client-id"
$clientSecret = "your-client-secret"
$tenantId = "your-tenant-id"

$SecureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($clientId, $SecureSecret)

Connect-AdminPowerAppEnvironment -TenantId $tenantId -Credential $Credential

Step 3: Retrieve Available Environments

To set a default environment, first list all available environments:

Get-AdminPowerAppEnvironment | Select-Object DisplayName, EnvironmentName, Location, EnvironmentSku

Note the EnvironmentName of the environment you want to set as default.


Step 4: Set the Default Power Platform Environment

To set a specific environment as the default, use:

Set-AdminPowerAppEnvironment -EnvironmentName "your-environment-id" -IsDefault

Replace "your-environment-id" with the EnvironmentName retrieved in Step 3.


Step 5: Verify the Default Environment

To check if the environment has been successfully set as default:

Get-AdminPowerAppEnvironment | Where-Object { $_.IsDefault -eq $true }

This should return the details of the new default environment.


Step 6: Disconnect from Power Platform

Once done, disconnect your session:

Disconnect-PowerAppsAccount

Leave a Reply

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