I came across a similar error using task Azure Powershell "Could not find the modules: 'Az.Accounts' with Version: '5.1.1' If the module was recently installed, retry after restarting the Azure Pipelines task agent"
.
My fix to this error is to use the latest version of azurePowerShellVersion
,
azurePowerShellVersion: 'latestVersion'
For classic view:
select the latest version 4.* of task Azure Powershell
check latest installed verison for azure powershell version
If above fix does not work for you. For better troubleshooting, please post the full error message and the detailed settings of Azure Powershell task.