WriteOutput help

Welcome Forums General PowerShell Q&A WriteOutput help

This topic contains 6 replies, has 3 voices, and was last updated by

Ian
 
Participant
4 years, 8 months ago.

  • Author
    Posts
  • #14266
    Ian

    Participant
    Points: 0
    Rank: Member

    $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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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
    Points: 0
    Rank: Member

    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.

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

  • #14270
    Ian

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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
    Points: 0
    Rank: Member

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

  • #14336
    Ian

    Participant
    Points: 0
    Rank: Member

    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"
    

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

The topic ‘WriteOutput help’ is closed to new replies.