?
Solved

PowerShell- How to write output to a text file?

Posted on 2007-04-03
5
Medium Priority
?
51,958 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 1000 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Suggested Courses

777 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