# How to get all Azure DevOps organizations in your AzureAD directory using PowerShell

I was in a situation where I needed to audit some DevOps-related data. What was my surprise when I realized that there is no programmatic way to get all existing Azure DevOps organizations (at least uncle Google didn't offer me any). 

Sure you can visit ```
https://dev.azure.com/<yourOrganizationName>/_settings/organizationAad
``` and download csv file with these information manually. 👇
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645526861954/TrzljbXxy.png)

But I needed a programmatic way, so I have created the PowerShell function [Get-AzureDevOpsOrganizationOverview](https://github.com/ztrhgf/useful_powershell_functions/blob/master/Azure/Get-AzureDevOpsOrganizationOverview.ps1).

---

# How Get-AzureDevOpsOrganizationOverview function works
There are two main parts:
- Authentication to Azure DevOps
- Getting the data

## Authenticate to Azure DevOps
It took me a while to realize that MSAL authentication is the way to go. 
I've found inspiration in the function GetCurrentContext (placed in ContextHelper.ps1) from a great auditing module [AzSK.ADO](https://azsk.azurewebsites.net/). 

For MSAL auth. I've used the [MSAL.PS](https://github.com/AzureAD/MSAL.PS) module's function ```Get-MsalToken```.

> 
So there is an obvious requirement and that is to have [MSAL.PS](https://github.com/AzureAD/MSAL.PS) module installed

The result 👇
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645526367707/sgGtA-aWS.png)

## Get the DevOps organizations data
I've used Developer mode (F12) in the Chrome browser to get the URI 👇
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645526650118/6CYJbGpqR.png)

And then just used it in the Invoke-WebRequest like this 👇
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645526734851/qDYtRujnp.png)

---

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645527654109/PD-leln1E.png)
# How to use it
- **Download **[Get-AzureDevOpsOrganizationOverview](https://github.com/ztrhgf/useful_powershell_functions/blob/master/Azure/Get-AzureDevOpsOrganizationOverview.ps1)
- **Dot source** 
- **Run**
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1645527560030/COHlb0pQp.png)


