This task is intended to delete the user folder from the public PC, and the login account is a public account as well.
You should turn on the Windows license verification in the intune.microsoft.com
Go 'intune.microsoft.com' > Devices > Scripts and remediations
# Detection script
$usersDirectory = "C:\\Users"
$specificUserFolders = @("cakeTest", "Public")
# Get a list of all user folders under C:\Users
$userFolders = Get-ChildItem -Path $usersDirectory | Where-Object { $_.PSIsContainer }
# Exclude the specific user folders
$userFoldersToDelete = $userFolders | Where-Object { $specificUserFolders -notcontains $_.Name } | Select-Object -ExpandProperty Name
# Print the list of user folders to be deleted
Write-Host "User profiles to be deleted:" $userFoldersToDelete -ForegroundColor Red
# Check if there are any user folders to be deleted and set exit code accordingly
if ($userFoldersToDelete.Count -gt 0) {
Exit 1
} else {
Exit 0
}
If you excute the 'Remediation script', you should throw the exit code as '1'.
# Detection script
$usersDirectory = "C:\\Users"
$specificUserFolders = @("cakeTest", "Public")
# Get a list of all user folders under C:\Users
$userFolders = Get-ChildItem -Path $usersDirectory | Where-Object { $_.PSIsContainer }
# Exclude the specific user folders
$userFoldersToDelete = $userFolders | Where-Object { $specificUserFolders -notcontains $_.Name } | Select-Object -ExpandProperty Name
# Print the list of user folders to be deleted
Write-Host "User profiles to be deleted:" $userFoldersToDelete -ForegroundColor Red
# Remediation script
function Delete-UserProfiles {
param (
[ValidateNotNullOrEmpty()]
[string[]]$userFolders,
[ValidateNotNullOrEmpty()]
[string]$directory
)
foreach ($userFolder in $userFolders) {
$userFolderPath = Join-Path $directory $userFolder
try {
Remove-Item -Path $userFolderPath -Recurse -Force
Write-Host "User profile folder deleted: $userFolderPath"
} catch {
Write-Host "Error deleting user profile folder ${userFolderPath}: $($_.Exception.Message)"
}
}
}
# Call the remediation function
Delete-UserProfiles -userFolders $userFoldersToDelete -directory $usersDirectory
# Print a success message
Write-Host "User profiles under C:\\Users (except for the specific user folders) have been deleted." -ForegroundColor Red
Once you delete the user folder and log on again with the user account, you will encounter this error message. You can click the 'Close' button to continue, and you will log on to this computer with a temporary user profile. This happens because even though you deleted the user folder, the Windows OS still remembers your account. To fix this or to restore the default user profile, some registry modifications are required.
https://learn.microsoft.com/en-us/mem/intune/fundamentals/remediations#prerequisites
https://scloud.work/user-profile-clean-up-intune/