none
PoSh: How to add the -Append parameter to the Tee-Object cmdlet in PoSh 2? RRS feed

  • Question

  • I have to support Vista systems in my test lab and have scripts that use the Tee-Object extensively.  Unfortunately, the Tee-Object in PowerShell 2.0 doesn't support the -Append parameter like it does in PowerShell 3.0 so the file I'm writing to isn't correct on Vista systems.

    What's the easiest way for me to enhance the PowerShell 2.0 Tee-Object cmdlet?  Can I just copy something from a system with PowerShell 3.0 on it and get it to work on a system with PowerShell 2.0 on it?

    Thursday, January 15, 2015 10:46 PM

Answers

  • Piping the output to the following seems to work (assuming you want to keep it all on one line):

    Dir | ForEach-Object -Process {Write-Output $_;$_|Out-File $myLogFile -Append}

    Friday, January 16, 2015 11:05 PM

All replies

  • Just upgrade to 3.0.


    ¯\_(ツ)_/¯

    Thursday, January 15, 2015 11:38 PM
  • Ah, I don't think you can install PowerShell 3.0 on a Vista system, at least not as far as I've been able to tell.
    Friday, January 16, 2015 12:36 AM
  • Ah, I don't think you can install PowerShell 3.0 on a Vista system, at least not as far as I've been able to tell.

    Vista is no longer supported,  You need to upgrade.

    See Vista end of life info here:

    http://support2.microsoft.com/lifecycle/?p1=11737&wa=wsignin1.0

    Vista died in 2012.

    You can build your own Tee-Object function.


    ¯\_(ツ)_/¯

    Friday, January 16, 2015 12:48 AM
  • Upgrading the system is not an option. It's a member of a software test bench and my script needs to run on it.

    Mainstream support for Vista may have expired but extended support doesn't end until 4/11/17. I work for an ISV and our policy is to test our software on all versions of Windows until extended support has ended as we have many small business customers who don't always upgrade their systems before Microsoft stops mainstream support.  So, we have to test our software on all systems that our customers are using, and that includes Vista.

    I know a custom Tee-Object function can be built, I'm just not sure how to do it (hence my op).

    Friday, January 16, 2015 3:26 AM
  • In my mind, Tee-Object simply takes two steps and makes it one. It shouldn't be too difficult to replicate it, just expect you'll have to do it with two lines. Those two steps are one, write to a file or set a variable, and two output the object, or pass it to the next cmdlet. We don't have your command, but here's an example.

    $a = Get-Process
    $a | Tee-Object -FilePath c:\procs.txt #Writes to a file, and writes to the host

    -becomes-

    $a = Get-Process
    $a | Out-File -FilePath c:\procs.txt -Append #Writes to a file
    Write-Output -Verbose $a #Write to the host

    Out-File should offer the -Append parameter in PS 2.0. If it doesn't, then try Add-Content whose whole purpose is to append data to an existing file. If you need help with modifying a specific command that includes Tee-Object, then be sure to share an example.
    Friday, January 16, 2015 5:00 AM
  • Piping the output to the following seems to work (assuming you want to keep it all on one line):

    Dir | ForEach-Object -Process {Write-Output $_;$_|Out-File $myLogFile -Append}

    Friday, January 16, 2015 11:05 PM