2

I'm new to Powershell and I've got a problem. I have this code below, after execution it prompts for a group name in AD and then exports all members of this group into .csv file. How to change this to import multiple group names from .csv file?

param
(   
    [Parameter(Mandatory=$true,position=0)]
    [String]$GroupName
)

$GFile = New-Item -type file -force "C:\Scripts\GroupDetails.csv"

$Groups = Get-ADGroup -filter {Name -like $GroupName} | Select-Object Name

ForEach ($Group in $Groups) {

    Get-ADGroupMember -identity $($group.name) -recursive | Select-Object samaccountname | Out-File $GFile -append
}

1 Answer 1

2

Maybe something like this would help you:

Param (
    [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
    [String]$ImportFile = 'S:\Test\Input_Test\GroupNames.txt',
    [ValidateScript({Test-Path -Path $_ -PathType Container})]
    [String]$SaveFolder = 'S:\Test\Input_Test'
)

$Groups = Get-Content $ImportFile

ForEach ($Group in $Groups) {
    Get-ADGroupMember -Identity $Group -Recursive | 
        Select-Object -ExpandProperty SamAccountName | 
            Out-File -LiteralPath (Join-Path $SaveFolder "$Group.txt")
}

You can fill the file GroupNames.txt with the groups you want and it will create one file for each group.

Content GroupNames.txt:

Group 1
Group 2

If you want to go a bit more advanced, you can create a function like this:

Function Export-ADGroupMembersToFile {
    Param (
        [Parameter(Mandatory)]
        [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
        [String]$ImportFile,
        [Parameter(Mandatory)]
        [ValidateScript({Test-Path -Path $_ -PathType Container})]
        [String]$SaveFolder
    )

    $Groups = Get-Content $ImportFile

    $OutFile = Join-Path $SaveFolder "Groups_$(Get-Date -F 'yyyyMMddHHmm').txt"
    Write-Verbose  "Output will be saved to file '$OutFile'"

    ForEach ($Group in $Groups) {
        Write-Verbose "GroupName: $Group"
        "`n", $Group, ('-'*40) | Out-File -LiteralPath $OutFile -Append

        $Members = Get-ADGroupMember -Identity $Group -Recursive | 
            Select-Object -ExpandProperty SamAccountName

        foreach ($M in $Members) {
            Write-Verbose "Adding user $M"
            Out-File -InputObject $M -LiteralPath $OutFile -Append
        } 
    }
}

Export-ADGroupMembersToFile -ImportFile 'S:\Test\Input_Test\GroupNames.txt' -SaveFolder 'S:\Test\Input_Test' -Verbose

This will output one file that contains all the GroupNames.

Sign up to request clarification or add additional context in comments.

3 Comments

Answer updated by your request. If you remove the -Verbose switch, you don't see the output. I guess a function is always better. It makes things portable. If you would like to change it and want to stop somewhere within a function, use F9 in the PowerShell ISE to set a PSBreakPoint. And it allows you to check the contents of variables.
Thanks a lot DarkLite1! The second one was a great base. After some modifications it works like a charm:)
Awesome! Glad it helped you out. And remember, keep on exploring! You get better at it every time you play with it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.