I have a script that currently runs great with results displayed in a console. I've been trying to figure out how to change the output to a file (.txt or .csv). I have created empty files and errors but no luck. I've had very limited experience so a suitable skill level solution/approach would be appreciated.
Can someone help me out?
Here is what I've got:
function CountMembers([string]$OU)
{
$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
##
$search.Filter = "(OU=$OU)"
$result = $search.FindAll()
$path = [ADSI]$result[0].path
$children = $path.psbase.children
$count = 0
##
foreach($child in $children)
{
if($child.objectcategory -like '*organizational*')
{
CountMembers($child.name)
}
elseif($child.objectcategory -like '*computer*')
{
[int]$count = $count + 1
}
}
if ($count -gt 0)
{
Write-Host "$OU | $count"
}
}