When I Write-Host
from my .ps1 file I see:
Parentfolder >> ChildFolder
When I output to a file, I see:
ParentFolder
>>
ChildFolder
I am using a simple write-host ($childgroup.name), ">>", ($object.samaccountname)
When I try to output the same information using Return
, Out-File
, Export to CSV
, etc... I get 3 lines for what Write-Host
prints as a single line.
I just want the output file to be in the same format as the Write-Host
output.
as requested:
function getchildgroups($groupname) {
# Get initial group details and members
$childgroup = get-adgroup $groupname -properties member
# Only continue if this group has members
if (($childgroup.member).count -gt 0) {
# Loop through each member of the group
foreach ($memberobject in $childgroup.member) {
try {
$object = get-adobject $memberobject -properties *;
# If the member of the group is another group
if ($object.objectclass -eq "group") {
# Print it to the screen
write-host ($childgroup.name),">>", ($object.samaccountname)
#$cgname = $childgroup.name
#$objname =$object.samaccountname
#Return (($cgname, ">>", $objname)) >>
c:\Temp\NestedGroups.txt
# Recursive lookup the members of the sub-group (if
not self-nested)
if ($memberobject -ne $object.distinguishedname) {
getchildgroups($object.distinguishedname);
}
}
} catch {}
}
}
}
# Run the function with your group name
$Groups = Get-Content C:\temp\ListOfFolders.txt
Foreach ($Group in $Groups){
getchildgroups("$group")
}