Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET

Deleting multiple files from multiple computers not working as expected

Home Forums Scripting PowerShell Deleting multiple files from multiple computers not working as expected

Viewing 1 post (of 1 total)
  • Author
    Posts

  • crocodile
    Member
    #617239

    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\Test1\*
    D$\Test2\Test1\*

    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 I am specifying each subfolder in the filelist, but that could be problematic in the future, as I would have to constantly monitor the folders to see if there are any subfolders added and then add them to the filelist. Ideally I need to only specify the folder, the script needs to go through all subfolders and delete all files, but not subfolders. 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 [email protected]@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

    • This topic was modified 2 years, 4 months ago by crocodile.
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: