Auto Delete Message From Yammer Community with PowerShell

Auto Delete Message From Yammer Community with PowerShell

I was recently tasked with clearing out a Yammer Communities auto-welcoming posts. If you are not familiar with auto-welcoming posts, Yammer used to automatically post a message whenever someone joined a community. It looked something like this, “Bradley Wyatt joined the Lazy Administrator network. Take a moment to welcome Brad.

Thankfully, Microsoft removed this feature so your community is not littered with auto welcoming messages anymore. But, if your community existed prior to this change you may be faced with hundreds, if not thousands of these posts that you may want to remove.

  1. Register a New App so we can interface with the Yammer API
  2. Next, click “Generate a developer token for this application.” Save the token for later as you will need it.
  3. Next, navigate to the Yammer Admin page and click “Export Network Data” under Content and Security.
  4. Select the date range you want to export the data from. In my case, we are just starting to use Yammer so I will export all data. Once you have selected the range, click Export.

  5. Unzip the folder it downloads and open the Mesages.csv file.
  6. In my example, I want to delete all message in the “All Company” community. The All Company community can be found as it will have no group_name associated to the messages. I can also delete automated messages by selecting messages generated by ‘system’ under message_type
  7. Use the script below and add your token you got earlier to the personalToken variable. Modify where your CSV lives as well. You can also feed it messageIDs one at a time if you would like.
    [string]$personalToken = '8623024-xd5QbsuvdWlvNKvg'
    Function Get-Token() {
        $headers = @{ Authorization=("Bearer " + $personalToken) }
        return $headers
    }
    Function Remove-YammerMessage($messageId) {
        $headers = Get-Token
        return Invoke-WebRequest –Uri "https://www.yammer.com/api/v1/messages/$($messageId)" –Method Delete -Headers $headers
    }
    
    #Grab all message IDs from my All Company community
    $messages = Import-Csv -Path "$env:HOME\Downloads\export-1661462628568\Messages.csv" | where-object {$_.group_name -eq ""}
    
    foreach ($i in $messages) { 
       Remove-YammerMessage $i.id
    }
  8. Going back to by Community after it has been ran, I can see that all my messages are deleted.

The PowerShell code source can be found at the following blog. Make sure to check them out!

 

Leave a Reply

Your email address will not be published.