# Restoring M365 users via MS Graph Powershell

**URL:** <https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401>\
**Category:** PowerShell Help\
**Created:** [May 31, 2023, 8:05am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401 "2023-05-31T08:05:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![compukortschnoi](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@compukortschnoi](https://forums.powershell.org/u/compukortschnoi)\
**Post date:** [May 31, 2023, 8:05am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/1 "2023-05-31T08:05:54Z")

</div>

Hi everyone,

I am looking for a way to restore M365 users in powershell on macOS. I can’t go through the admin center because there the restore is only possible under certain conditions (original domain and license type still exists etc.).

Unfortunately, the good old module MSOnline isn’t available in Powershell under macOS .

Can I somehow manage the recovery via the Graph Powershell? Unfortunately ChatGPT spits out only nonsense, or refers erroneously to MSOnline.

In the end I only need equivalents for the following commands:

```auto
Connect-MsolService
Get-MsolUser -ReturnDeletedUsers
Restore-MsolUser -UserPrincipalName "davidchew@contoso.com" -NewUserPrincipalName "davidchew02@contoso.com" -AutoReconcileProxyConflicts

```

How do I get this to work with MS Graph Powershell?

Kind regards,  
compukortschnoi

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [May 31, 2023, 9:27am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/2 "2023-05-31T09:27:33Z")

</div>

Hi, welcome to the forum 👋

The MSOnline module is deprecated on Windows too. Everyone is advised to migrate to the Microsoft Graph PowerShell SDK before 30th June 2023. After that date, MSOnline and other older modules are no longer supported and you may find your scripts stop working as Microsoft switch off older APIs.

You can find mappings between old and new commands here:

> **[Find Azure AD and MSOnline cmdlets in Microsoft Graph PowerShell](https://learn.microsoft.com/en-us/powershell/microsoftgraph/azuread-msoline-cmdlet-map?view=graph-powershell-1.0)**
>
> Use this map of the Azure AD PowerShell and MSOnline cmdlets to find their Microsoft Graph PowerShell equivalents.

---

<div class="post-metadata">

**Author:** ![compukortschnoi](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@compukortschnoi](https://forums.powershell.org/u/compukortschnoi)\
**Post date:** [June 1, 2023, 8:23am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/3 "2023-06-01T08:23:56Z")

</div>

Hi Matt,

thank you for your reply and the warm welcome. 🙂

I have studied the table and tried to rewrite the above lines for the Graph Powershell:

```auto
Connect-MgGraph -Scope Directory.ReadWrite.All
Get-MgUser -All | Format-List ID, UserPrincipalName
Restore-MgDirectoryDeletedItem -DirectoryObjectId a01234bc56789d0d12f34567e8f90f1g

```

The problems start in the second line. I only get a list of active users. There does not seem to be a -ReturnDeletedUsers switch. How can I get a display of the deleted users?

When I run the third line, I get the following error:

“Restore-MgDirectoryDeletedItem\_Restore1: Insufficient privileges to complete the operation.”

I don’t understand this, I did give myself sufficient authorization in line 1. Of course I am global admin too.

Does anyone have any ideas?

Kind regards,  
compukortschnoi

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 1, 2023, 9:45am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/4 "2023-06-01T09:45:05Z")

</div>

`Get-MgUser` won’t show deleted users, you need to use `Get-MgDirectoryDeletedItem`.

There is a good guide to using that here:

> **[Delete and Recover Azure AD User Accounts with PowerShell](https://office365itpros.com/2022/03/23/delete-azure-ad-user-accounts/)**
>
> This article explains how to delete Azure AD user accounts and recover them using cmdlets from the Microsoft Graph PowerShell SDK.

Be sure to read that thoroughly and, if necessary, read the linked GitHub discussions to understand how to view the data that’s returned. It might look like you’re not getting back any data, but you will be.

If you check the restore query in Graph Explorer, you will see that the scope `Directory.ReadWrite.All` is not listed. Therefore, it cannot perform a restore action. You should choose one of the following scopes:

```auto
AdministrativeUnit.ReadWrite.All
Application.ReadWrite.All
Group.ReadWrite.All
User.ReadWrite.All

```

> **[Graph Explorer | Try Microsoft Graph APIs - Microsoft Graph](https://developer.microsoft.com/en-us/graph/graph-explorer)**
>
> Try the Graph Explorer developer tool to learn about Microsoft Graph APIs. Includes code snippets, Microsoft Graph Toolkit, and Adaptive Cards integration.

---

<div class="post-metadata">

**Author:** ![compukortschnoi](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@compukortschnoi](https://forums.powershell.org/u/compukortschnoi)\
**Post date:** [June 1, 2023, 11:24am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/5 "2023-06-01T11:24:43Z")

</div>

Unfortunately, it does not work with any of the four scopes, the error message remains the same. 😕

I will read the first link at my leisure, thank you!

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 1, 2023, 11:38am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/6 "2023-06-01T11:38:05Z")

</div>

> [@compukortschnoi](#):
>
> Unfortunately, it does not work with any of the four scopes, the error message remains the same.

Before posting, I tested it with `Directory.ReadWrite.All` and `User.ReadWrite.All` and it worked only when using the latter.

Did the account to be restored have any privileged admin roles? If so, you may also need `Directory.AccessAsUser.All`.

> **[Restore deleted item (directory object) - Microsoft Graph v1.0](https://learn.microsoft.com/en-us/graph/api/directory-deleteditems-restore?view=graph-rest-1.0&tabs=http)**
>
> Restore a recently deleted application, group, service principal, or user from deleted items.

---

<div class="post-metadata">

**Author:** ![compukortschnoi](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@compukortschnoi](https://forums.powershell.org/u/compukortschnoi)\
**Post date:** [June 6, 2023, 9:31am UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/7 "2023-06-06T09:31:50Z")

</div>

Hi Matt,

please forgive my late reply.

You put me on the right track: the right scope led to success. The restore was successful. 😍

Also the script at [Delete and Recover Azure AD User Accounts with PowerShell](https://office365itpros.com/2022/03/23/delete-azure-ad-user-accounts/) to list the deleted users worked for me, but after I corrected line 23. Uncorrected it throws an error concerning sort and a broken pipe, so I changed it from

```auto
$Report | Sort {$_.Deleted -as [datetime]} | Format-Table UserId, Name, Deleted, "Days Since Deletion", Type -AutoSize

```

to the following:

```auto
$Report | Format-Table UserId, Name, Deleted, "Days Since Deletion", Type -AutoSize

```

Now I also get a list of all deleted users. Yee-haw!

Thank you for your help!

Kind regards,  
compukortschnoi

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/restoring-m365-users-via-ms-graph-powershell/22401/8 "2024-05-16T20:14:53Z")

</div>


