Redirect and move the Downloads folder to OneDrive

Redirect and move the Downloads folder to OneDrive

Introduction

In general, the Downloads folder is intended for temporary files and should not be redirected to OneDrive. However, for the particular use case described in this post, moving Downloads to OneDrive can improve the end user experience by providing better integration between two different environments.

Use case

Organizations are modernizing their way of working and often implementing Intune with (cloud-native) Azure AD Joined devices. Some of these organizations still have some “Legacy” apps that may not be supported to run locally and are instead offered as a Published Application, for example, through Azure Virtual Desktop.

With OneDrive Known Folder Move, organizations can already redirect and move the Desktop, Documents, and Pictures to OneDrive. However, the Downloads folder cannot be included by default. Having different user profiles in different environments comes with challenges. For example, when a user is working in a Published App and exports a file, it could be that the file is saved to the Downloads folder by default.

When users then open their local Windows Explorer, they’ll find that the files saved to their Downloads folder that was accessed through the Published app aren’t available.

In order to better integrate the different user profiles, it is worth considering redirecting the Downloads folder to OneDrive.

Note: Redirecting the Downloads folder may or may not suit your organization’s use case. There are disadvantages in redirecting the Downloads folder. For example Downloads can quickly become a mess and items may be redirected that shouldn’t be redirected to OneDrive in the first place.

Automatically clean up old downloads

To prevent OneDrive’s from growing because of a lot of Downloads, it’s possible to automatically clean up files older than X number of days. I’ve included an option for how to do that in this post. Don’t worry: when the Downloads folder is redirected to OneDrive, users will also be able to restore (automatically) deleted Downloads from their OneDrive recycle bin when necessary.

How to redirect & clean up the Downloads folder?

In the next chapters, you’ll learn:

  • How to redirect the Downloads folder to OneDrive on Intune managed devices.
  • How to redirect the Downloads folder to OneDrive via Group Policy on AD Joined session hosts for your Published Apps.
  • How to automatically clean up old Downloads with Proactive Remediations

Redirecting the Downloads to OneDrive with Intune

For redirecting the Downloads folder to OneDrive with Intune, I wrapped a PowerShell script with the Microsoft Win32 Content Prep Tool, so it can be deployed as an application with Intune.

PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "Move-DownloadsFolderToOnedrive.ps1" -OneDriveOrgName "%OrgName%"

Replace %OrgName% with your organization’s name that is displayed in the Windows Explorer next to the OneDrive Icon. This is for example “Contoso”.

  • For your Uninstall command, use:
PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "Move-DownloadsFolderToUserProfile.ps1"
  • Select User for the Install behavior.
  • Download and modify the Detect-DownloadsFolderToOneDrive.ps1 file from GitHub and replace %OrgName% in Line 1 and 2 with your organization’s name.
  • Select Use a custom detection script as a Detection rule and upload your modified Detect-DownloadsFolderToOneDrive.ps1 file.
  • Assign the app to users or devices.

Your app is ready! When deployed to the users, the Downloads folder will be redirected to OneDrive.

Redirecting the Downloads to OneDrive with Group Policy Objects (GPOs)

On AD Joined devices, you can redirect the Downloads folder with Group Policy Objects (GPOs).

  • Create a Group Policy Object (GPO) and edit it.
  • Navigate to User Configuration > Preferences > Windows Settings > Environment.
  • Right-click Environment, and then select New > Environment Variable.
  • In the Action section, select Replace and make sure User variable is selected.
  • In the Name section, enter the name for the environment variable, for example: OneDriveSync and then in the Value box, enter the path to OneDrive including the OneDrive Org Name, for example %USERPROFILE%\OneDrive – Contoso
  • Click OK.
  • Navigate to User Configuration > Policies > Windows Settings > Folder Redirection.
  • Right-click the Downloads folder, and then select Properties.
  • In the Properties dialog box, from the Setting box, select Basic – Redirect everyone’s folder to the same location.
  • In the Target folder location section, select Redirect to the following location and then in the Root Path box, enter the path to OneDrive including the folder name where Downloads needs to be redirected to, for example: %OneDriveSync%\Downloads
  • Optionally select the Settings tab to modify settings to your preference.
  • Click OK and exit the Group Policy Management editor.

Cleaning up old Downloads with Proactive Remediations

In Intune, you can add a Proactive remediation that automatically cleans up old items in your downloads folder. New to Proactive remediations? Check out the Tutorial: Proactive remediations.

Below is an example of Detection & Remediation scripts available to use in your Proactive remediation. You can run this daily and it will clean up downloads older than 30 days. Make sure to run this Proactive remediation using the logged-on credentials.

Detection script

try {
    $downloadsFolder = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path

    if (Get-ChildItem -Path $downloadsFolder | Where-Object { $_.lastWriteTime -lt (Get-Date).AddDays(-30)}) {
        Write-Host 'Found files in the Downloads Folder older than 30 days, deleting these files ...'
        exit 1
    }
    else {
        Write-Host 'No files in the Downloads folder were found that are older than 30 days. Does not need remediation.'
        exit 0
    }
}
catch {
    Write-Error $_.Exception.Message
    exit 1
}

Remediation script

try {
    $downloadsFolder = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
    Get-ChildItem -Path $downloadsFolder -Recurse | Where-Object { $_.lastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction Stop
    Write-Host 'Deleted files in Downloads folder that were older than 30 days'
    exit 0
}
catch {
    Write-Error $_.Exception.Message
    exit 1
}

Leave a Reply

Your email address will not be published. Required fields are marked *