Solaire
Solaire

Reputation: 23

Remove-Item is not removing any files

I am trying to remove a few files from over 80 servers on my network. The code runs fine and even says that the files are being deleted, however when I go and check the servers.... the file are still there. I am not quite sure where to go from here.

(This is what my code tells me

The file is still in the folder

Here is my code below:

$filelist = @("/Contracts/Contract.con",
           "/Contracts/DefaultFinanceSale.con",
           "/Contracts/DefaultRentalReturnOrder.con",
           "/Contracts/EPOSSchedule.con",
           "/Contracts/SMSAuthorization.con",
           "/Tags/BarCode.tag",
           "/Tags/Sample 4x6.TAG",
           "/Tags/Sample FullPage.TAG",
           "/Tags/Sample FullPageBundle.TAG",
           "/Tags/Sample FullPageBundle2.TAG",
           "/Tags/Sample FullPagePackage.TAG",
           "/Tags/Sample FullPagePackage2.TAG",
           "/Tags/Sample(2).tag")

$computerlist = Get-Content C:\support\scripts\server_list.txt
$Log = "c:\support\scripts\logs\Test_Delete_Old_Files_$(Get-Date -Format 'yyyyMMddhhmmss').log"

Start-Transcript -path $Log -append -Force -NoClobber
foreach ($file in $filelist){
    foreach ($computer in $computerlist){
        Write-Host -ForegroundColor Yellow "Analysing $computer"

    $newfilepath = Join-Path "\\$computer\" "$file"
    if (test-path $newfilepath){
        Write-Host "$newfilepath file exists"
        try
        {
            Get-ChildItem $newFilePath -Force -Recurse | %{$_.Attributes = "readonly"} -ErrorAction Stop | Remove-Item
        }
        catch
        {       
            Write-host "Error while deleting $newfilepath on $computer.`n$($Error[0].Exception.Message)"
                   
        }
        Write-Host "$newfilepath file deleted"
    } else {
        Write-Information -MessageData "Path $newfilepath does not exist"
    }
}

} Stop-Transcript

Upvotes: 0

Views: 677

Answers (2)

Theo
Theo

Reputation: 61068

You should reverse the two foreach loops, and go through the $computerlist first, otherwise you keep switching between computers for every file in the list.

Then, as commented, your $filelist variable contains partial paths fo Files, not directories. Since a file does not have any child items like a ditrectory or disk can have, the Get-ChildItem will not find anything.

To also remove items that are set Read-Only (and/or Hidden), you can use the -Force switch on Remove-Item.

Try using a nested loop like this:

foreach ($computer in $computerlist) {
    Write-Host -ForegroundColor Yellow "Analysing $computer"
    foreach ($file in $filelist) {
        $newfilepath = Join-Path -Path "\\$computer" -ChildPath $file
        if (Test-Path -LiteralPath $newfilepath -PathType Leaf) {
            try {
                Remove-Item -LiteralPath $newfilepath -Force -ErrorAction Stop
                Write-Information "$newfilepath file deleted on computer '$computer'"
            }
            catch {
                Write-Warning "Error while deleting $newfilepath on computer '$computer'.`r`n$($_.Exception.Message)"
            }
        }
        else {
            Write-Information -MessageData "File $newfilepath does not exist on computer '$computer'"
        }
    }
}

Upvotes: 1

Luis Adames
Luis Adames

Reputation: 52

Have you tried adding the -Force to the end of the remove-item ?

Upvotes: 1

Related Questions