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 6345587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:51:03+00:00 2026-05-24T20:51:03+00:00

is there an easy way in powershell to output a string to variable and

  • 0

is there an easy way in powershell to output a string to variable and console at the same time?

i want to capture the output of my script to a variable so i can analyze it in the end of the script, save it to a log file and also email to an operator.

my intent is to have a variable $output and add any output strings to it and also output to console immediately something like

$output="Process started"

$output=+"Doing step 1"
"Doing step 1"

$output=+"Doing step 2"
"Doing step 2"

so in the end I can save $output to a log file, email it and parse it.

I played with tee-object that might work for that purpose but unfortunately it would rewrite my $output variable instead of appending a string to it.

UPDATE
This is the final solution I decided to go with – thanks to manojlds!

$script:output = ""

filter mylog {
    $script:output+= $_+"`n"
    return $_
}


"doing step {0}" -f 1 | mylog
"doing step {0}" -f 2 | mylog
"doing step {0}" -f 3 | mylog

#in the end of the script
$script:output
  • 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-05-24T20:51:04+00:00Added an answer on May 24, 2026 at 8:51 pm

    There are so many ways to get your end goal:

    In your script just have something like this:

    "Process started"
    <step1>
    "Doing step 1"
    <step2>
    "Doing step 2"
    ...
    

    Then run the script as

    .\test.ps1 | Tee-Object -file log.txt
    

    Note that the output is available to Tee-Object and hence the console as and when it occurs. You don’t get the output only after the entire script runs. This is how pipeline works in Powershell. Objects are passed along downstream as and when they occur. Insert a sleep 10 in between as steps and see that the output comes as soon as it is available.

    Also, you don’t necessarily have to have another script ( the launcher.ps1 ) you are talking about. You can use functions, scriptblock etc. within your script.

    Some other ways:

    function test {
    
    "Process started"
    sleep 5
    "Doing step 1"
    sleep 5
    "Doing step 2"
    
    }
    
    test | %{$output+=$_;$_}
    #use output
    write-host -fore blue $output
    

    You can create a filter:

    $script:output = ""
    
    filter appendOutput {
    
        $script:output+= $_
        return $_
    }
    
    "Process started" | appendOutput
    sleep 5
    "Doing step 1" | appendOutput
    sleep 5
    "Doing step 2" | appendOutput
    #use output
    write-host -fore blue $script:output
    

    There are probably many more ways of doing this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way in PowerShell to format numbers and the like in
Is there an easy way to tell if a ruby script is already running
Is there an easy way to capitalize the first letter of a string and
Is there an easy way (like a free program) that can covert c/c++ code
Is there an easy way to iterate over an associative array of this structure
Is there an easy way in C# to create Ordinals for a number? For
Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there an easy way to avoid dealing with text encoding problems?
Is there an easy way to set the zoom level for a windows form
Is there an easy way to read an entire Access file (.mdb) into a

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.