Common parameter -WhatIf - used for previewing a command's actions - never outputs data; instead, the preview information is printed straight to the console, meaning it can neither be sent through the pipeline, nor redirected to file, nor captured.
As an aside: Remove-Item without -WhatIf also never produces data output, so there is generally no point in trying to process its output in a subsequent pipeline segment.
Your best bet is to add an -OutVariable (-ov) common parameter to your Where-Object call, which allows you to export the collected file-info objects via Export-Csv in a separate command:
Get-ChildItem ... | Where-Object -OutVariable filesToRemove { ... } |
Remove-Item -WhatIf ...
$filesToRemove | Export-Csv D:\output.csv
The above still prints the preview information to the console, but also collects the [System.IO.FileInfo] objects selected by Where-Object in variable $filesToRemove, which you can then export in CSV format.
-WhatIfin general, but it looks like the information is printed straight to the console, not to the verbose output stream (number4).[string]object output to the1stream)1is the success (data) output stream, but this is not where-WhatIfoutput goes - as far as I can tell, you cannot capture / redirect it all from within PowerShell. (You could from the outside, such as when calling fromcmd.exe).5(Information) likeWrite-Hostdoes in PSv5