How to delete users in PagerDuty using REST API v2 and PowerShell
First get a API Key, skip if you already have a API Key.
Log into your PagerDuty instance, go to Configuration > API Access
Click on Create New API Key
Add a Description, then select Create Key
You should now have a PagerDuty API key
We will need to get a list of all users, in a past post I built a function (Get-PagerDutyUsers) to get list of all users in PagerDuty. We will need to get the UsersID for the user we want to delete.
Sample of output of Get-PagerDutyUsers
From the output the user we want to delete is CITLead Test1 with the UserID of P5EZCF8.
PagerDuty has an excellent API reference
Under the section for Users > Delete a User they give us hints on how we can code this in PowerShell. We will be using the Invoke-RestMethod, and we will start with the header.
In the API reference page, we can try out the API calls, when click try PagerDuty shows all actions.
Also keep note of the method of "DELETE"
We need to transform it for the Invoke-RestMethod, this is how it should look like
Here is a example code
Lets build a function.