Powershell : Group info

Hello Experts,

I need to output sidHistory values for the group along with ManagedBy. There are multiple SIDHistory Values in some groups, so I need to output all the values for that group. I have the script below, which only outputs 1 SIDHistory Value.

$MyTempGroups =  gc ./groups.txt
$result = @()
foreach($group in $MyTempGroups) {
$x = get-adgroup $group -properties sidhistory, ManagedBy, | select ManagedBy, sidHistory
$result += $x.ManagedBy +  ";"  + $x.sidHistory
$result | out-file ./results.txt

Your assistance is appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You should get a single record with a (space separated( list of SIDS already.
But your script doesn't make much sense to me as is. You are concatenating all data, not including the respective group.
What is your actual expected result if using multiple groups and having done more than one domain migration for the group objects? Can you show a short example output?
Parity123Author Commented:
The output needs to be as follows:

GroupName; ManagedBy; sid1,sid2

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I don't have any SID histories on my groups in AD, but the following should work.

It definitely works for a null set of SIDs like I have and "should" work for a set of SIDs in your sid history.

I figured you wanted to know the current SID too, so it is the 1st sid listed in the output.

This will check all groups in the AD, but you can easily amend this to meet your needs

Let me know if any issues.

get-adgroup -filter * -properties sid, sidhistory, ManagedBy | % { $Output="$($_.name);$($_.ManagedBy);$($_.Sid)"; foreach ($SIDH in $_.SIDHistory) { $Output="$Output,$SIDH" }; $Output }

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
get-content groups.txt |
  % { get-adgroup $_ -properties managedby } |
  % { $_.name, $_.sid, $_.managedby, ($_.sidhistory -join ',') -join ';' } |
  out-file results.txt

Open in new window

is what I would use.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Parity123Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.