WriteOutput help

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by  Ian 3 years, 4 months ago.

  • Author
    Posts
  • #14266

    Ian
    Participant

    $searchOption =[IO.SearchOption]::AllDirectories
    $fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
    foreach($fileName in $fileEntries)
    {
    [Console]::WriteLine($fileName);
    }

    How do i change the code above to make it output to a text file?

  • #14267

    Don Jones
    Keymaster

    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.

  • #14268

    Ian
    Participant

    [quote=14267]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.
    [/quote]

    Thanks for the quick reply Don. I will give it a try and let you know how it goes.

  • #14270

    Ian
    Participant

    I was able to get it to work by adding the -append switch. Thanks Don Jones!

    $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" -Append

    }

    /endthread

  • #14282

    Dave Wyatt
    Moderator

    On a side note, you can just pipe $fileEntries straight to Out-File, with no need for your own foreach loop:

    $fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
    
    $fileEntries | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt"
    
  • #14269

    Ian
    Participant

    [quote=14267]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.
    [/quote]

    $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. 🙁

  • #14336

    Ian
    Participant

    [quote=14282]On a side note, you can just pipe $fileEntries straight to Out-File, with no need for your own foreach loop:

    $fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
    
    $fileEntries | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt"
    

    [/quote]

    Hello Dave, i tried it your way and that works as well! Thanks for providing an alternate solution!

You must be logged in to reply to this topic.