Link to home
Start Free TrialLog in
Avatar of Flexcar
Flexcar

asked on

PowerShell- How to write output to a text file?

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
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


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
Avatar of Flexcar
Flexcar

ASKER

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."
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Flexcar

ASKER

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

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