Piping from get-MgUserMailFolder to Remove-MgUserMailFolder throws an error and it is not accepted

Copper Contributor

Hello All,


I've connected to MgGraph using an AccessToken and this access token has read,writeAll permissions on all mail folders throughout the business.


I have roughly 400 users that I need a folder removed from their Outlook. The folder name is "Promo"

Import-csv pathToFile.csv |foreach { {get-MgUserMailFolder -UserId $_.PrimarySmtpAddress -Filter "Displayname eq 'Promo'"} ==>Works. I can see all of the ID and displayName.


When I pipe it to remove it is complaining that "The variable '$_' cannot be retrieved because it has not been set"

import-csv c:\pathToFile.csv | foreach-object {get-MgUserMailFolder -UserId $_.PrimarySmtpAddress -Filter "Displayname eq 'Promo'"} | Remove-MgUserMailFolder -MailFolderId $_.ID
From searching the help on those commands, they both do not accept pipeline input based on ID. How do i remove the Mailfolder ID's identified from the 1st command?


1 Reply
Use a proper script instead of the pipeline - most MG cmdlets are autogenerated and nothing about them is designed with ease of use in mind.