Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:57:20+00:00 2026-06-02T16:57:20+00:00

I have a foreach loop inside my powershell script with prints $output on the

  • 0

I have a foreach loop inside my powershell script with prints $output on the shell during each iteration. There are lot many outputs and the number of entries that the shell can display is limited. I am looking to export the output to a text file. I know how to do it in command line. How is it possible in a powershell though?

FYI, I am using a batch script from the command line to run the powershell script as

powershell c:\test.ps1 c:\log.log 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:57:23+00:00Added an answer on June 2, 2026 at 4:57 pm

    You can always redirect the output an exe to a file like so (even from cmd.exe):

    powershell c:\test.ps1 > c:\test.log
    

    Within PowerShell, you can also redirect individual commands to file but in those cases you probably want to append to the log file rather than overwrite it e.g.:

    $logFile = 'c:\temp\test.log'
    "Executing script $($MyInvocation.MyCommand.Path)" > $logFile
    foreach ($proc in Get-Process) {
        $proc.Name >> $logFile
    }
    "Another log message here" >> $logFile
    

    As you can see, doing the redirection within the script is a bit of a pain because you have to do lots of redirects to file. OTOH, if you only want to redirect part of the output to file then you have more control this way. Another option is to use Write-Host to output info to the console meant for someone observing the results of the script execution. Note that Write-Host output cannot be redirected to file.

    This is an example executed from CMD.exe

    C:\Temp>type test.ps1
    $OFS = ', '
    "Output from $($MyInvocation.MyCommand.Path). Args are: $args"
    
    C:\Temp>powershell.exe -file test.ps1 1 2 a b > test.log
    
    C:\Temp>type test.log
    Setting environment for using Microsoft Visual Studio 2008 Beta2 x64 tools.
    Output from C:\Temp\test.ps1. Args are: 1, 2, a, b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to have a foreach loop where the initial array is changed inside
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
I have a foreach loop in jQuery going through each 'div.panel', I need to
I have the following inside a foreach loop (displaying my various videos), I'm trying
I have placed a execute sql task inside the foreach loop container , where
I have a nested while loop inside a foreach loop where I would like
I have a question regarding db queries inside of a foreach loop. Im making
I have a foreach loop inside a table to display the content from DB
If I have a foreach loop, is there any way to check a boolean
I use a foreach loop inside ASP.NET MVC View page. For each element of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.