Score:1

Task Scheduler GPO for purging files does not apply due to OneDrive

us flag

I am trying to create a task with the following PS script:

$locations="$env:userprofile\Desktop\New folder (2)","$env:userprofile\Desktop\New folder (3)"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
foreach ($location in $locations) {Get-ChildItem $location -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item}

The new folders were created for testing, eventually it should be Downloads folder.

When running script locally, using path C:\Users\name.lastname\Desktop... the script works fine and deletes the files in the correct directory. However, I have to check manually by going to C:\Users\name.lastname\Desktop... to find that out. The "New folder (2)" and "New folder (3)" on my desktop still have the files (which are older than 30 days as the script is written) after applying the GPO to my machine. When I check the folder path (C:\Users\name.lastname\OneDrive - tekexperts.onmicrosoft.com\Desktop) I started to suspect that the variable $env:userprofile is finding the One Drive synced folders on my Desktop and consequently not deleting anything.

I would really appreciate it if somebody can advise if it is possible for my script to search for the exact proper system folders, instead of synced ones. Thank you in advance.

cn flag
You may want to try this: https://stackoverflow.com/a/64256803/175990
vidarlo avatar
ar flag
How do you run it? is `$env:userprofile` what you expect it to be?
us flag
Hi, when I run it locally without GPO I use the path C:\users\name.surname\..... and so on. However, when I create the test through the Computer Settings configuration I must use a variable $env:userprofile in order for the GPO to apply to all the required users' machines. If there is any other way I would appreciate the help. Thank you for the response!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.