Powershell log deleted files

11,491

Solution 1

You've got a good start here:

write-host “Deleting File $File” -foregroundcolor “Red”

Unfortunately Remove-Item doesn't have any output that you can mooch from, but you've already made your own output message so we can just build from that. You can pipe any output to a file by using Out-File. The append flag will attach the new content to the end of the file, and you do not have to check if the file exists.

Write-Output “Deleting File $File” | Out-File -Append logfile.txt

You don't even have to include Write-Output, if you want a shorter line.

Here is an example that shows where you need to add code. I've marked existing code with "...", and I've moved the deletion message into a variable so that you can reuse it at another location. This assumes that you've stored the selected filename in a variable.

...
if ($files.Count -gt $keep) 
{
    ...
    $message = "Deleting File $File at "+(Get-Date)
    $message | Out-File -Append logfile.txt 
}
...

Solution 2

First you will need a log-message type function in your script that will log the message to a .log file. Then chekc if the file exists and if not then create a file.

Then just before you delete your file with Remove-Item command you can use Log-Message function to log message to the log file.

% { (Log-Message "Deleting File $_"); $_ }

Complete script

$path = "C:\test\1"
$keep = 3
$strLogFileName   = "c:\test\yourlogfile.log";

function Log-Message
{
   Param ([string]$logtext)
   Add-content $strLogFileName -value $logtext
}

$dirs = Get-ChildItem -Path $path -Recurse | Where-Object {$_.PsIsContainer}
foreach ($dir in $dirs) {
    $files = Get-ChildItem -Path $dir.FullName | Where-Object {-not $_.PsIsContainer -and $_.name -like "*.zip"}
    if ($files.Count -gt $keep) {
        $files | Sort-Object CreationTime -desc| Select-Object -First ($files.Count - $keep) | 
        % { $dt=get-date;(Log-Message "Deleting File $_  on  $dt");$_ }| Remove-Item -Force 

    }
}
Share:
11,491

Related videos on Youtube

zero1de
Author by

zero1de

Updated on September 14, 2022

Comments

  • zero1de
    zero1de over 1 year

    The script searches all folders and subfolders and delete the oldest file when the number of files is>5. Everything works fine, but I want also log all the delete Files as a record in a log-file.

    How can I log the deleted files ?

    Here the Script.

    $path = "C:\test\1"
    $keep = 3
    $strLogFileName   = "c:\test\yourlogfile.log";
    
    $dirs = Get-ChildItem -Path $path -Recurse | Where-Object {$_.PsIsContainer}
    foreach ($dir in $dirs) {
        $files = Get-ChildItem -Path $dir.FullName | Where-Object {-not $_.PsIsContainer -and $_.name -like "*.zip"}
        if ($files.Count -gt $keep) {
            $files | Sort-Object CreationTime -desc| Select-Object -First ($files.Count - $keep) | Remove-Item -Force 
        ***{write-host “Deleting File $File” -foregroundcolor “Red”; Remove-Item $File | out-null}*** 
        }
    }
    
  • zero1de
    zero1de over 10 years
    O.k seem like perfect but can you tell how can i add the Datetime for the delete file for each file to into the Log ??
  • zero1de
    zero1de over 10 years
    you are really god with powershell :) One last question, how can I delete only the oldest file and not the newest ? Many thx for your help
  • zero1de
    zero1de over 10 years
    Thx i have found it: Sort-Object LastWriteTime
  • zero1de
    zero1de over 10 years
    I need your help once again. Now the zip files should not be deleted but the folders which are one level higer. Here the Folder path:\\xxx\xu_207\Archiv\20080101 can you help me again ? Many thx
  • Anthony Neace
    Anthony Neace over 10 years
    @zero1de You should ask this new problem in a new question and upvote and/or accept answers that solved your previous problem. Stringing Mitul along without crediting him appropriately is very rude and may discourage users from helping you in the future.