Solved

PowerShell- How to write output to a text file?

Posted on 2007-04-03
5
51,697 Views
Last Modified: 2012-05-05
Hello.

I am struggling with trying to get my Active Directory queries to export to a text file so that I can use them as input for other scripts. I'm sure I'm missing the obvious because this has to be an easy thing to do. I have tried using Out-File. With that I only get the first item in the query. Here's what I have so far (writes to screen):

#
$Dom = 'LDAP://ou=Distribution,ou=Groups,ou=staff,dc=whitehous,dc=gov'
$Root = New-Object DirectoryServices.DirectoryEntry $Dom
cls
Write-host "PowerShell connects to domain: $Dom `n"

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root

# Filter the users with -like "CN=*". Note the ForEach loop
$adobj= $selector.findall() `
| where {$_.properties.objectcategory -like "CN=*"}
foreach ($Group in $adobj){
$prop=$Group.properties
Write-host "$($prop.cn)"
}
Write-host "`n`n"
Write-host  "There are $($adobj.count) Groups in the $($root.name) OU."

Many thanks!

Eric
0
Comment
Question by:Flexcar
  • 3
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18848985

Hi Eric,

I've had much more luck using Write-Output then piping into a file using Out-File (purely to give me choice about where I send output).

In the example above I replaced:

Write-host "$($prop.cn)"

With:

Write-Output "$($prop.cn)"

Then called the script with:

./<Script> | Out-File <FileName>

Write-Host is certainly less useful in these situations as it does exactly what you ask and writes it only to the host UI.

HTH

Chris
0
 

Author Comment

by:Flexcar
ID: 18852595
Hello Chris.
Thank you very much for your help on this. I'm getting the same results as I was with Out-File, the output file only shows 1 (of 134) groups. Here's the code I have so far:
$Dom = 'LDAP://ou=Distribution,ou=Groups,dc=cia,dc=gov'
$Root = New-Object DirectoryServices.DirectoryEntry $Dom
cls
Write-host "PowerShell connects to domain: $Dom `n"

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root

# Filter the users with -like "CN=*". Note the ForEach loop
$adobj= $selector.findall() `
| where {$_.properties.objectcategory -like "CN=*"}
foreach ($Group in $adobj){
$prop=$Group.properties
Write-Output "$($prop.cn)" | Out-File Domain_DL-Groups.txt
}
Write-host "`n`n"
Write-host  "There are $($adobj.count) Groups in the $($root.name) OU."
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 18852624

You would have to pipe the output from the entire script out to Out-File - otherwise it'll just overwrite the same file each time.

Are you running it as a script? Or just typed directly? If it's typed it would explain why I couldn't quite see the same issues :)

Basically, I created this as a script:

$Dom = 'LDAP://ou=Distribution,ou=Groups,dc=cia,dc=gov'
$Root = New-Object DirectoryServices.DirectoryEntry $Dom
cls
Write-host "PowerShell connects to domain: $Dom `n"

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root

# Filter the users with -like "CN=*". Note the ForEach loop
$adobj= $selector.findall() `
| where {$_.properties.objectcategory -like "CN=*"}
foreach ($Group in $adobj){
$prop=$Group.properties
Write-Output "$($prop.cn)"
}
Write-host "`n`n"
Write-host  "There are $($adobj.count) Groups in the $($root.name) OU."


Saved that as a .ps1 file, then did:

./ScriptName.ps1 | Out-File <FileName>

Chris
0
 

Author Comment

by:Flexcar
ID: 18853024
Chris,
Thanks very much! I still don't understand what the difference is between running the commands through the UI vs. running the script, but that is what I was getting stuck on.

It works great now.  :)
Eric
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18853208

Good stuff :)

The only difference really is the batching up of the output. You can still do that by writing it in directly (I think) but its easier to control output in a script (or at least until I fully understand how it deals with that).

Chris

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now