Automating the cleaning of that directory after 21 days

I have a similar question but have some code to provide and get feedback…
Changing hostnames to protect the innocent

# Specify X days
$days = (get-date).adddays(-21)
# specify path to root folder
$path = "\\servername.domain.com\share\folder\folder"

#exclude folders & files by name
$exclude1 = "FOLDER1"
$exclude2 = "FOLDER2"
$exclude3 = "FOLDER3"
$exclude4 = "FILE01.lnk"
$exclude5 = "SCRIPT01.bat"

# format date
$date = $date = get-date -uformat "%m/%d/%Y"
# replace \ by -
$date = $date.ToString().Replace("/", "-")

$a= Get-ChildItem  $path | Where{$_.LastWriteTime -lt $days }

$a | foreach-object{
# expand the condtion if more folders needs to be excluded
if (($_ -notlike $exclude1) -and ($_ -notlike $exclude2) -and ($_ -notlike $exclude3) -and ($_ -notlike $exclude4) -and ($_ -notlike $exclude5))
{
$b = $_
write-host $b
}
# specify log path
$output =  "\\SERVER.domain.com\share\FileFolder\Logs" + "\" + "DEL" + "_" + "$date" + "_log.txt"
Add-content $output "$b directory has been deleted from TEMP..."
# remove hash file if you want to delete the files
$b | Foreach-Object { del $_.FullName -recurse}
}
########################################################################

This script was working well until something happened. The script has not been changed or modified except to add an additional $exclude. I tried deleting that $exclude and the issue persists.
I am currently receiving an error,

"Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\XShooting\CleanupUSMT.ps1:38 char:27:38 char:27
+ $b | Foreach-Object { del $_.FullName -recurse}
+                           ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand"

I am trying everything I have and know how to debug this and I can’t see why the variable is not being passed correctly ?

Any input is appreciated and I thank you for your time and help in advance!

You have been memeber for long enough to know that we do not hijack other peoples threads. :face_with_raised_eyebrow:

That’s impossible

Regardless of that:

Your code is quite confusing. What are you actually looking for? Files? Folders?
If you’re looking for files you cannot tell Remove-Item (that’s the cmdlet behind the alias “del”) to -Recurse!!

And you should not save your scripts in the folder C:\Windows\System32 !!!

To debug your script and figure out why it is failing you could simply output the variables to check their content. According to your error message - obviously one of your childitems does not have a FullName property.

My apologies, you’re right…I should not have hijacked the thread.

I’ll re-post this after I have done some more.

To clarify:
I am cleaning out a directory on a server share that receives USMT backups regularly…just automating the cleaning of that directory after 21 days. (IF directory or file is 21+ days old, delete the file and all folders (including files inside directories) There is another simple script in that directory to clean up any files and place them into new directories to keep it cleaned up (a bit) I don’t want that script to get deleted and working on moving that to a better location once I figure out how to do so.

I 100% agree that scripts should not be run from the C:\Windows\system32 directory…I am not running this script at all from that directory but a different server share altogether (tools)

I will re-post after combing through all the outputs to determine next steps.

Much appreciated for your feedback.

:wink: :smiley: … the error message you posted and you changed now revealed it. Why else would I have mentioned it? :wink: :smiley:

You should not mix data files and scripts in the same folder. It’s a goog idea to separate them. :+1:t4:

I’d recommend to run this script on the server where the share is - not remotely.
Why do you have to check the directory AND the files? I don’t have experience using USMT but shouldn’t be enough to check the metadata on one particular file or folder representing the creation time or the last access time or the last write time (whatever is relevant for your use case)?