Share via


Powershell delete multiple files from multiple computers not working exactly as I require

Question

Tuesday, May 14, 2019 9:17 PM

I have this script that I have compiled from different scripts to get exactly what I need.

The filelist looks something like this:

D$\Test1\

D$\Test2\

Now I want to delete the file in all of the subfolders of Test1 and Test2, but not actually delete any subfolders. At this stage the script does not recognise that there are files in the subfolders, so I get "No files to delete" in the log and nothing is deleted. The file count is also not working as expected, either reporting 1 or 0, which I guess is counting the folder and not the files. Any help to improve this would be highly appreciated.

function Write-Log($string)
{
    $outStr = "" + $Timestamp +" "+$string

    Write-Output $outStr

   }

$Timestamp = Get-Date -Format "yyyy-MM-ddTHH-mm-ss"
$filelist = Get-Content C:\support\scripts\filelist.txt
$computerlist = Get-Content C:\support\scripts\computerlist.txt
$Log = "c:\support\scripts\logs\Delete_Old_Files_$(Get-Date -Format 'yyyyMMddhhmmss').log"

Start-Transcript -path $Log -append -Force -NoClobber
Write-Log " Start of Log  "

foreach ($file in $filelist) {
    foreach ($computer in $computerlist){
        Write-Log "Analysing $computer"

        $newfilepath = Join-Path "\\$computer\" "$file" 
        if (test-path $newfilepath) {
            Write-Log "$newfilepath folder exists"
            try
            {
                Remove-Item $newfilepath -Recurse -force -ErrorAction Stop | Where-Object { -not ($_.psiscontainer) }
            }
            catch
            {       
                $ErrorMessage = $_.Exception.Message
                $FailedItem = $_.Exception.ItemName
                Send-MailMessage -From aaa@@aaa.com -To [email protected] -Subject "Old Files Delete Failed!" -SmtpServer bla.com -Body "The error message is: '$ErrorMessage'"
                Break
                       
            }
            Write-Log "$newfilepath files deleted"
        } else {
            Write-Log "Path $newfilepath no files to delete"
        }
        # output statistics
  Write-Output "**********************"
  #Write-Output "Number of old files deleted: $($_.Count)"
  Write-Log " End of Log "
    }
}


 Stop-Transcript

All replies (6)

Wednesday, May 15, 2019 2:19 AM ✅Answered

You have to retrieve only the files and not the folders.

Get-ChildItem <path> -File

help get-childitem -online

Read the docs carefully to learn how to use the command.

\(ツ)_/


Tuesday, May 14, 2019 9:34 PM

See this thread for a discussion of how to correct enumerate and access files on multiple folders.

https://social.technet.microsoft.com/Forums/windowsserver/en-US/d3617b59-3660-4830-b886-88860210cca5/archiving-old-file-from-multiple-folders?forum=winserverpowershell

A couple of simple changes and the proposed script will do what you ask.

\(ツ)_/


Wednesday, May 15, 2019 1:08 AM

Thanks, I have read your link, but I do not fully understand it.

I have changed the Filelist to individual subfolders:

D:\Test1\Test1\

D:\Test1\Test2\

D:\Test2\Test1\

etc.

This works, but still not really what I need. The subfolders may change from time to time, so I will constantly need to add subfolders to the filelist


Wednesday, May 15, 2019 1:14 AM

So what is your exact question?  Just add items to the list as needed.  That sounds more like an issue of using notepad to edit a file.

\(ツ)_/


Wednesday, May 15, 2019 2:14 AM

I want to know how to change my script to only specify the folder level, and it then deletes all files in subfolders, but does not delete the subfolders


Wednesday, May 15, 2019 2:47 AM

Thank you