Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Function - Change writehost to save to file

$
0
0

Hi.

I need to have list of all nested groups in an OU. How do I change output from screen to saving output to a file? I found the answer here - but I cannot convert it to my script - http://stackoverflow.com/questions/9294221/write-host-export-to-a-file

import-module activedirectory

$GroupList = @{}

$indent = ""

 

function Get-GroupHierarchy

{

    param

        (

            [Parameter(Mandatory=$true)]

            [String]$searchGroup

        )

 

    $groupMember = get-adgroupmember $searchGroup | sort-object objectClass -descending

    foreach ($member in $groupMember)

    {

        Write-Host $indent $member.objectclass,":", $member.name,$member.description;

        if (!($GroupList.ContainsKey($member.name)))

        {

 

            if ($member.ObjectClass -eq "group")

            {

                 $GroupList.add($member.name,$member.name,$member.description)

                 $indent += "`t"

                 Get-GroupHierarchy $member.name

                 $indent = "`t" * ($indent.length - 1) 

            }

        }

        Else

        {

            Write-Host $indent "Group:" $member.name "has already been processed, or there is loop... Please verify."  -Fore DarkYellow

        }

    }

}

# Simple Example of how to use the function
#$groupname = Read-Host -Prompt "Enter a groupname"

# Output the result to the file groupmemberships.csv
$Path = "C:\temp\"

# Get the group name, which we'll use to name the HTML File.
$file = $path + 'DisplayNestedGroups.csv'

 
$Groupname = Get-ADgroup -Filter * -searchbase "ou=groups,dc=domain,dc=net" | foreach {Get-GroupHierarchy $_} | export-csv -path "$file" -NoTypeInformation -Encoding "UTF8"


Viewing all articles
Browse latest Browse all 6937

Trending Articles