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!