Automating with PowerShell - Move user's onedrive files to an archive folder on SharePoint when offboarding employee

Kevin Carbonaro 21 Reputation points
2022-08-17T11:21:09.027+00:00

Currently we go through a manual process of;

  1. login to office365 with global admin.
  2. go to the user's properties and create onedrive link to the user's files.
  3. click the link, select all files/folders then move them to a sharepoint archive folder

During the process we have to leave the browser open otherwise the move will be cancelled.

Is there a way to do this using PowerShell? This part is the only thing which stops us from completely automating the offboarding procedure.

Thanks for any help.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,999 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,584 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lief Eric Malone 396 Reputation points
    2022-08-17T15:40:46.803+00:00

    Hi Kevin,

    I understand you need to preserve OneDrive files for archival purposes but you're currently going through a manual process to preserve these files so that they are no deleted after a departing employee leaves and their MS365 account is deleted.

    I believe the solution to your issue is to simply leave the files where they are, increase retention (available up to 10 years) and possibly enable automatic delegation. This way there is no manual work.

    Please find documentation here: https://learn.microsoft.com/en-us/onedrive/retention-and-deletion

    To setup retention -- Go to Settings in the new SharePoint admin center, and sign in with an account that has admin permissions for your organization. Click manage retention and choosing the number of days to retain the data.

    sp-retention.png

    To setup delegation -- ensure that users' managers are set in Azure AD, then complete the following:

    1. Go to More features in the new SharePoint admin center, and sign in with an account that has admin permissions for your organization.
    2. Under User profiles, select Open.
    3. Under My Site Settings, select Setup My Sites.
    4. Next to My Site Cleanup, make sure Enable access delegation is selected.
    5. Select OK.

    Once completed, this will free you from manual archival of user OneDrive data --- it will be retained for up to 10 years and it will be automatically available to the manager of the departed employee.

    If this answer was helpful, please accept it as an answer. It's appreciated.

    Blessings,
    Eric

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Emily Du-MSFT 48,646 Reputation points Microsoft Vendor
    2022-08-18T06:33:07.813+00:00

    @Kevin Carbonaro

    Based on your description, I understand that you want to pre-provision OneDrive for all licensed users in your organization.

    Please run below PowerShell.

    $Credential = Get-Credential  
    Connect-MsolService -Credential $Credential  
    Connect-SPOService -Credential $Credential -Url https://contoso-admin.sharepoint.com  
      
    $list = @()  
    #Counters  
    $i = 0  
      
      
    #Get licensed users  
    $users = Get-MsolUser -All | Where-Object { $_.islicensed -eq $true }  
    #total licensed users  
    $count = $users.count  
      
    foreach ($u in $users) {  
        $i++  
        Write-Host "$i/$count"  
      
        $upn = $u.userprincipalname  
        $list += $upn  
      
        if ($i -eq 199) {  
            #We reached the limit  
            Request-SPOPersonalSite -UserEmails $list -NoWait  
            Start-Sleep -Milliseconds 655  
            $list = @()  
            $i = 0  
        }  
    }  
      
    if ($i -gt 0) {  
        Request-SPOPersonalSite -UserEmails $list -NoWait  
    }  
    

    Reference: https://learn.microsoft.com/en-us/onedrive/pre-provision-accounts


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Limitless Technology 44,531 Reputation points
    2022-08-17T15:14:18.317+00:00

    Hello there,

    Found this script online to transfer OneDrive data to another user.

    $departinguser = Read-Host "Enter departing user's email"  
    $destinationuser = Read-Host "Enter destination user's email"  
    $globaladmin = Read-Host "Enter the username of your Global Admin account"  
    $credentials = Get-Credential -Credential $globaladmin  
    Connect-MsolService -Credential $credentials  
       
    $InitialDomain = Get-MsolDomain | Where-Object {$_.IsInitial -eq $true}  
        
    $SharePointAdminURL = "https://$($InitialDomain.Name.Split(".")[0])-admin.sharepoint.com"  
        
    $departingUserUnderscore = $departinguser -replace "[^a-zA-Z]", "_"  
    $destinationUserUnderscore = $destinationuser -replace "[^a-zA-Z]", "_"  
        
    $departingOneDriveSite = "https://$($InitialDomain.Name.Split(".")[0])-my.sharepoint.com/personal/$departingUserUnderscore"  
    $destinationOneDriveSite = "https://$($InitialDomain.Name.Split(".")[0])-my.sharepoint.com/personal/$destinationUserUnderscore"  
    Write-Host "`nConnecting to SharePoint Online" -ForegroundColor Blue  
    Connect-SPOService -Url $SharePointAdminURL -Credential $credentials  
        
    Write-Host "`nAdding $globaladmin as site collection admin on both OneDrive site collections" -ForegroundColor Blue  
    # Set current admin as a Site Collection Admin on both OneDrive Site Collections  
    Set-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true  
    Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true  
        
    Write-Host "`nConnecting to $departinguser's OneDrive via SharePoint Online PNP module" -ForegroundColor Blue  
        
    Connect-PnPOnline -Url $departingOneDriveSite -Credentials $credentials  
        
    Write-Host "`nGetting display name of $departinguser" -ForegroundColor Blue  
    # Get name of departing user to create folder name.  
    $departingOwner = Get-PnPSiteCollectionAdmin | Where-Object {$_.loginname -match $departinguser}  
        
    # If there's an issue retrieving the departing user's display name, set this one.  
    if ($departingOwner -contains $null) {  
        $departingOwner = @{  
            Title = "Departing User"  
        }  
    }  
    

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.