Deleting certain files + output to screen in one line?

  • I've been trying to get a "oneliner" to work for the purpose of traversing through a directory, finding all files with a certain extension, property, lastaccess or what else is useful

    I would then want the script to delete the files and also output which files are deleted

    I created a directory C:\powershell\ with few subdirectories just for the testing.

    The below works fine just for deleting, but I haven't been able to work out how to get a list of which files have been deleted into the same code.

    Get-ChildItem c:\powershell -recurse | where {$_.Extension -eq ".mp3"} | ForEach ($_) {Remove-Item $_.fullname -WhatIf}

    Anyone got any pointers?

  • found the solution - just posting it here in case it can help somone in the future

    Get-ChildItem c:\powershell -recurse | where {$_.Extension -eq ".mp3"} | ForEach ($_) {$_.fullname; Remove-Item $_.fullname -WhatIf}

    Just found the option to add 2 actions seperated with semicolon to the foreach statement:

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply