Show output on screen and in file in PowerShell

Use the Tee-Object cmdlet.

The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.

dir | Tee-Object -file dir.txt

You should use it like,

 ForEach ($OU In $OUs)
 {
     $OU.distinguishedName
     Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
         -Filter * | Select Name
 } | Tee-Object -file c:\temp\outfile.txt

Note: It has an alias, tee, which is the same as Unix’ tee.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)