35

How can I get all this to not only output on the screen, but to save to a text file in CSV format?

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) { $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * | Select Name } 

I have tried

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName ForEach ($OU In $OUs) { $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * | Select Name } | | export-CSV c:\temp\outfile.csv –noType 

And many other formats, but I always get the error:

An empty pipe element is not allowed.

4 Answers 4

80

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.

Sign up to request clarification or add additional context in comments.

7 Comments

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName ForEach ($OU In $OUs) { $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * | Select Name } Tee-Object -file c:\temp\outfile.txt
I am and i am getting the same error }| Tee-Object -file c:\temp\outfile.txt An empty pipe element is not allowed. At line:8 char:3
i got it to work with Tee but i had to (and i will try to use the right terms) wrap that in { } and maike it a function. I was then able to Tee the function. Thanks
Tee-Object has not exactly the most meaningful name Microsoft could've given the cmdlet. But then, it is Powershell..
Unfortunately Tee-Object does not have an -Encoding option so it always outputs in UTF-16, which many tools don't support properly.
|
2

This works:

Get-WmiObject -Class Win32_Product | Select Name, Version | Sort Name | Tee-Object -file h:\InstalledApps.TXT 

1 Comment

How do we append to file instead of overwrite it? Use the -Append parameter of Tee-Object
1

An empty pipe element is not allowed.

You have a duplicate bar in the last line:

} | | export-CSV c:\temp\outfile.csv –noType 

That doesn't fix the error completely, because you can't pipe directly from the foreach loop. You can export it another way to a CSV file instead of a text file like Tee-Object does (you could convert Tee-Object to CSV). This way, it outputs the results to a CSV file and prints the results to the screen:

$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" ` -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName $results = @() ForEach ($OU In $OUs) { $OU.distinguishedName Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel ` -Filter * | Select Name } $results | Export-Csv c:\temp\outfile.csv -NoTypeInformation write-host $results 

This can also be done with a much simpler script:

Get-ADComputer -SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" -SearchScope OneLevel -Filter * | Select Name | Export-Csv c:\temp\outfile.csv -NoTypeInformation Import-Csv c:\temp\outfile.csv 

Comments

1

tee-object is nice but very limited. for those who need more control over the function - use write-log function from eNLib

install-module eNLib

write-log enables to fork text, objects, define logfile with start-logging function, doesn't require to define logfile name each time - very useful for advanced scripting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.