You’re writing directly to the console. It isn’t intended to be redirected.
Use Write-Output instead. That writes to the pipeline and can be redirected.
Or, if you only want to write to a file, use Out-File.
$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
foreach($fileName in $fileEntries)
{
$fileName | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt"
}
I was able to get it to out to file with your suggestion. The only issue is it only outputs 1 line instead of listing all the files in that given directory.