1

I have the following script, that works fine, listing the candidate files to be remove. But I cannot figure it out where or how to add the Remove-Item instruction to actually delete the files, without losing the export file. By the way, a Write-Host log wouldn't satisfy as I need to record size of files, last write time, etc. It must be a CSV export (as the one I have with this script).

Of course I can re-run the Get-ChildItem and instead to pipe out to Select-Object, pipe out to Remove-Item, but it would imply a great loss of time.

My script:

$EXTERNALFILE = "C:\DataCleaning\Scripts\FilesToRemove.txt"
$DESTINATION_DIR = "C:\DataCleaning\Logs\"
$LOGFILECSV = "${DESTINATION_DIR}\Removed_$(Get-Date -f yyyy-MM-dd_hh-mm-ss).csv"

$List = Get-Content $EXTERNALFILE

$REPORT = foreach ($Data in $List) {
    $Data = $Data -split(';')
    $SOURCEDIR = $Data[0]
    $FILTERMASK = $Data[1]
    $RETENTION = (Get-Date).AddMonths(-$Data[2])

    Get-ChildItem -LiteralPath $SOURCEDIR -File -Recurse |
        Where-Object {($_.LastWriteTime –lt $RETENTION) -and ($_.Extension -notmatch $FILTERMASK)} |
        Select-Object FullName, LastWriteTime, CreationTime,
            @{Name="Size (MB)";Expression={[Math]::Round($_.Length /1MB, 2)}},
            @{Name="Age (Days)";Expression={(((Get-Date) - $_.LastWriteTime).Days)}}
}

$REPORT | Export-Csv -Delimiter "|" -NoTypeInformation -Encoding UTF8 -Path $LOGFILECSV
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
mundodisco
  • 11
  • 1
  • Keep in mind that `Remove-Item` can use the `-WhatIf` switch. – lit Oct 23 '18 at 22:45
  • Yes Lit, good tip! The -WhatIf parameter is extremely useful and time-saver! What I didn't know was the the ".FullName" parameter after my $REPORT variable (when I put the dot, there appears many suggestions, but not "FullName"). (that is concerning user Nas' answer). – mundodisco Oct 24 '18 at 15:02
  • When you put the dot, you get a list of properties for the `$REPORT` **array**, not for the array **items**. What we are doing is *member enumeration* https://stackoverflow.com/questions/12131416/how-does-member-enumeration-work-in-powershell-3 – Nas Oct 24 '18 at 18:10

1 Answers1

1

There are multiple solutions, something straightfoward:

Remove-Item -Path $REPORT.FullName

Remove-item accepts an array of strings for the -path parameter

Remove-Item [-Path] <string[]>

If you want to perform the Remove-Item inside the foreach loop:

Get-ChildItem -LiteralPath $SOURCEDIR -File -Recurse |
    Where-Object ... |
        ForEach-Object {
            Remove-Item $_
            $_
        } | Select-Object ...
Nas
  • 1,243
  • 6
  • 7
  • So Nas, you suggest adding a new line after the export. I will try that and comment. But I was thinking of adding the "Remove-Item" inside the ForEach loop, as a pipe ("|") or something like that, so that the item (the file) is removed right after being "indexed" (selected). Am I clear? – mundodisco Oct 24 '18 at 13:40
  • Nas, Ia have just tried your suggestion (Remove-Item -Path $REPORT.FullName) and I can confirm it works! What I cannot understand is why when I insert the dot, FullName does not appear in the list of accepted parameters. – mundodisco Oct 24 '18 at 14:54
  • However, I still would like to know how to properly insert the Remove-Item inside the loop, piped or something like that (as I said in my first comment). – mundodisco Oct 24 '18 at 14:55
  • Check my edited post. I also added a comment to your post concerning your other question. – Nas Oct 24 '18 at 18:11