Solved

PowerShell- How to write output to a text file?

Posted on 2007-04-03
5
51,882 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 71

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 71

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 71

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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