Solved

PowerShell- How to write output to a text file?

Posted on 2007-04-03
5
51,720 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

862 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

25 Experts available now in Live!

Get 1:1 Help Now