thepip3r
thepip3r

Reputation: 2935

PowerShell: Can you grab parts of a upstream, piped command for output?

If I'm using PowerShell Cmdlets like:

Get-ADGroup -Server "my-dc" -Filter {name -like "*Blue*1"} | Get-ADGroupMember | Out-File $output

Is there a way that I can output the found group name inside of the text file as well? Currently, this only outputs the group members into the file.

Upvotes: 0

Views: 798

Answers (3)

ravikanth
ravikanth

Reputation: 25800

Another variant:

Get-ADGroup -Filter {Name -like "*demo*"} | % { "GroupName: $($_.Name)"; Get-ADGroupMember $_ } | Out-File C:\Scripts\Group.txt

This will have something similar in the text file:

GroupName: DemoUsers

distinguishedName : CN=Ravikanth,CN=Users,DC=BarCamp,DC=in name : Ravikanth objectClass : user objectGUID : c4257f39-c84e-43e3-adb2-dfb6d13a8f2a SamAccountName : Ravikanth SID : S-1-5-21-4177501474-3918321425-3674396201-1000

Upvotes: 1

Chad Miller
Chad Miller

Reputation: 41767

Get-ADGroup -Server "my-dc" -Filter {name -like "*Blue*1"} | Get-ADGroupMember | foreach { $_.name | out-file -FilePath $output -Append}

Upvotes: 0

slipsec
slipsec

Reputation: 3062

Try this:

Get-ADGroup | %{
    # Here $_ is the group, do with it what you will :)
    $_
    $_ | get-ADGroupmember
}

Upvotes: 0

Related Questions