Script seems correct, but seemingly not working

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 6 years, 5 months ago.

  • Author
  • #5820

    by notarat at 2013-04-18 06:25:48

    I'm using the following script to delete some files over 6yrs old from my network share and it doesn't seem to be working.

    I'm looking at the chosen drive, ignoring MS Access files, then exporting the information about the file I am trying to delete so I have a record of what was deleted, then deleting the item.

    $strDRV = Read-Host 'Enter Drive to Remove Aged Data From: Ex: C:\ '
    $strOF = Read-Host 'Enter Path and Filename for Output: Ex: C:\outputfiles\sales_Deleted.csv '
    Write-Host "Attempting to Delete the Following Aged Files From: "$strDRV
    Get-ChildItem $strDRV -exclude *.mdb -recurse| where {!$_.PSIsContainer -and $_.Lastwritetime -lt (date).adddays(-2190)}| Select PSPath,Length,LastWriteTimeUTC | Export-CSV $strOF |remove-item -force

    It exports the list of old files properly, but it doesn't seem to actually remove the old files.

    Am I missing something here? It seems correct

    by happysysadm at 2013-04-18 06:35:26

    Hi Notarat,

    you are mixing things up. You are piping export-csv to remove-item. That won't work.

    You should modify the script to put all the filenames you want to delete in a variable (such as $files_to_remove), then pipe its content first to remove-item then to export-csv.

    Think also to choose more readables names for your variables, i.e. $drive instead of $trdrv and $csvlog instead of $strOF.

    Tell me if you need more help understanding these instructions.


    by mjolinor at 2013-04-18 06:45:04

    Export-CSV doesn't pass it's input on down the pipeline, and doesn't have a -passthru option so it can only be used at the end of the pipeline.

    Same for remove-item.

    Beyond that, remove-item is expecting pipeline input to either be a path string, or an object with a Path property. You're trying to pass an object that only has a PSPath property for the file identity, which it won't understand. I'd use FullName instead of PSPath.

    $strDRV = Read-Host 'Enter Drive to Remove Aged Data From: Ex: C:\ '
    $strOF = Read-Host 'Enter Path and Filename for Output: Ex: C:\outputfiles\PAO_Deleted.csv '
    Write-Host "Attempting to Delete the Following Aged Files From: "$strDRV

    $files = Get-ChildItem $strDRV -exclude *.mdb -recurse|
    where {!$_.PSIsContainer -and $_.Lastwritetime -lt (date).adddays(-2190)}
    Select Fullname,Length,LastWriteTimeUTC

    $files |
    Export-CSV $strOF

    $files |
    select -ExpandProperty Fullname |
    remove-item -force

    by happysysadm at 2013-04-18 06:49:32

    Hi Mjolinor,

    this is what I meant and when I said "pipe its content first to remove-item then to export-csv" I should have added " on another line".

    Thanks for clarifying better then I did though!

    by mjolinor at 2013-04-18 07:15:34

    I had actually started composing that before you replied, and got interrupted in the middle of it, so hadn't seen your reply yet when I posted it.

    by notarat at 2013-04-18 07:30:17

    Weird...I replied and it didn't show up.

    Reposting it to say thanks to everyone! This site and the people here are the best! I've learned more from the few posts I've made here than I ever could have from reading every book on the subject.

    Happy, I understood your post. Adding the export option was a last minute rushed thing that my boss wanted so I could show exactly which files I deleted in case someone else deleted an important file and tried to point fingers at me. When I added it, I mistakenly though that the information piped to the export would also go to the remove-item. (n00b mistake on my part for making that assumption without asking) I also updated all my variables to be easier to read. I usually started all of them with str for some reason and it just makes more sense to use plain language

    mjolinor, thanks for providing the example code! It allows me to see the differences in how it handles the values as they are passed through the script.

    You guys are awesome!

You must be logged in to reply to this topic.