This worklet will delete the contents of all users’ Download folders on a system. It also checks the location if the folder was remapped into OneDrive.
If you do not want it touching OneDrive, you’ll want to remark out (or remove) the following lines from the evaluation and remediation code:
$ODtargetFolder = “OneDrive\$targetFolder”
$folderList += “$profile\$ODtargetFolder”
Evaluation:
<#
.SYNOPSIS
Delete Contents of Specified Folder for All Users on a System
OS Support: Windows 8 / Server 2008 R2 SP1 and above
Powershell: 3.0 and above
Run Type: Evaluation
.DESCRIPTION
This worklet is designed to search for any contents located in $targetFolder & $ODtargetFolder (its' possible location in OneDrive) for each user on a system, and delete the contents. If any contents are found making it non-compliant, the evaluation script will close with an
exit code of '1' to trigger remediation. $targetFolder & $ODtargetFolder need to be set the same for evaluation and remediation to work correctly.
To use this worklet, add the desired parameters to the $targetFolder & $ODtargetFolder lines at the top of this script.
.NOTES
Author: twiese
Date: June 8, 2021
#>
# Location of folder contents to delete in profile and OneDrive
$targetFolder = "Downloads"
$ODtargetFolder = "OneDrive\$targetFolder"
# Query WMI and get a list of all user profile locations
$userProfiles = (Get-WmiObject win32_userprofile).LocalPath | Where-Object { $_.Substring(0,8) -EQ "C:\Users" }
# Build a list of all possible locations
$folderList = @()
foreach ($profile in $userProfiles) {
$folderList += "$profile\$targetFolder"
$folderList += "$profile\$ODtargetFolder"
}
# Check each location for potential files
foreach ($folder in $folderList) {
# Check each target folder for existance, then for contents
if (Test-Path $folder -PathType Container) {
if ((Get-ChildItem $folder -Recurse | Measure-Object).Count -gt 0) {
# If folder found with contents, exit for remediation
Exit 1
}
}
}
# No contents found in target folders so exit without remediation
Exit 0
Remediation:
<#
.SYNOPSIS
Delete Contents of Specified Folder for All Users on a System
OS Support: Windows 8 / Server 2008 R2 SP1 and above
Powershell: 3.0 and above
Run Type: Evaluation
.DESCRIPTION
This worklet is designed to search for any contents located in $targetFolder & $ODtargetFolder (its' possible location in OneDrive) for each user on a system, and delete the contents. If any contents are found making it non-compliant, the evaluation script will close with an
exit code of '1' to trigger remediation. $targetFolder & $ODtargetFolder need to be set the same for evaluation and remediation to work correctly.
To use this worklet, add the desired parameters to the $targetFolder & $ODtargetFolder lines at the top of this script.
.NOTES
Author: twiese
Date: June 8, 2021
#>
# Location of folder contents to delete in profile and OneDrive
$targetFolder = "Downloads"
$ODtargetFolder = "OneDrive\$targetFolder"
# Query WMI and get a list of all user profile locations
$userProfiles = (Get-WmiObject win32_userprofile).LocalPath | Where-Object { $_.Substring(0,8) -EQ "C:\Users" }
# Build a list of all possible locations
$folderList = @()
foreach ($profile in $userProfiles) {
$folderList += "$profile\$targetFolder"
$folderList += "$profile\$ODtargetFolder"
}
# Check each location for potential files
foreach ($folder in $folderList) {
# Check each target folder for existance, then for contents
if (Test-Path $folder -PathType Container) {
if ((Get-ChildItem $folder -Recurse | Measure-Object).Count -gt 0) {
# Delete contents of folder
Get-ChildItem $folder -Recurse | ForEach { Remove-Item $_.FullName -Force -Recurse }
}
}
}