# Need help formatting

**URL:** <https://forums.powershell.org/t/need-help-formatting/21432>\
**Category:** PowerShell Help\
**Created:** [January 20, 2023, 8:48pm UTC](https://forums.powershell.org/t/need-help-formatting/21432 "2023-01-20T20:48:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [January 20, 2023, 8:48pm UTC](https://forums.powershell.org/t/need-help-formatting/21432/1 "2023-01-20T20:48:02Z")

</div>

Hello there!

I have below script to get the details of computer objects containing ‘usa’.

All of these objects are either part of one of the update groups :  
“Update-group-00”,  
“Update-group-01”,  
“Update-group-10”,  
“Update-group-11” or  
can have 2 or 3 groups due to human error.

To identify that, I have below script, but it’s not sufficient for what I’m looking. It gives unformatted output with ‘@{}’ or would give only one group even few computer accounts are added to 2 or more Update groups.

```auto
Get-ADComputer -Filter * -Properties * | Where-Object {

    $_.MemberOf -like "*Update-Group*" -and

    $_.Name -like "*usa*"

} |

Select-Object Name,@{

    N="Groups";

    E={

        $groups=""

        $_.MemberOf | get-adgroup | ?{$_.Name -like "*Update-Group*"}|Select-Object Name | ForEach-Object {$groups+=$_.Name+","}

        $groups -replace ",$",""

        }

    } 

```

Expected output to be like:

Name Group  
---------. -------------------------  
Abcusa1. Update-Group-00  
Cdusa2. Update-Group-00; Update-Group-01

Can someone please help me

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 20, 2023, 11:20pm UTC](https://forums.powershell.org/t/need-help-formatting/21432/2 "2023-01-20T23:20:28Z")

</div>

There are a few issues with our code …

> [@Rahul20](#):
>
> `Get-ADComputer -Filter * -Properties * `

That puts a lot of stress to your DC. I’d recommend to use a `-SearchBase` to limit the query to a certain branch where your computer accounts are. And it will very likely speed up your code.  
Next - it’s best practice to filter as far left as possible. This will speed up your code as well.  
And you should only query the properties you need - not all of them.

So this part of your code could look like this:

```auto
$SearchBase = 'OU=Computer,OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=de'
Get-ADComputer -SearchBase $SearchBase -Filter "Name -like '*usa*'" -Properties MemberOf

```

> [@Rahul20](#):
>
> `$_.MemberOf -like "*Update-Group*" `

Since the property `MemberOf` returns an array this will not work. You will have to use the complete distinguished name of the group and you should use the comparison operator `-contains` … like this:

```auto
Where-Object -Property MemberOf -Contains -Value 'CN=Update-Group,OU=Groups,OU=Germany,OU=Europe,DC=contoso,DC=de'

```

> [@Rahul20](#):
>
> ```auto
> $groups=""
> $_.MemberOf | get-adgroup | ?{$_.Name -like "*Update-Group*"}|Select-Object Name | ForEach-Object {$groups+=$_.Name+","}
> $groups -replace ",$",""
> }
> 
> ```

That part confuses me … do you have more than one update group? … anyway … try this:

```auto
Select-Object Name,@{
    Name = 'UpdateGroupList'
    Expression = {
        ($_.MemberOf |
            Get-ADGroup |
                Where-Object -Property Name -like -Value '*Update-Group*').Name -join ', '
        }
    }

```

Of course you have to connect all individual snippets with a pipe to make it work. 😉 👆🏽

Regardless of all that … could you please try to avoid to post that muc hunnecessary white space in your code. It makes it much harder to read.  
Thanks in advance.

* * *

* * *

**Edit:**  
If you really have more than one “_Update Group_” you want to compare against you should put all groups with their dinstinguished names in an array and use `Compare-Object` to check if one or more of these groups are contained in the “MemberOf” property. … it could look like this:

```auto
$UpdateGroupList = @(
    'CN=Update-Group,OU=Groups,OU=Germany,OU=Europe,DC=contoso,DC=de'
    'CN=Update-Group,OU=Groups,OU=France,OU=Europe,DC=contoso,DC=de'
    'CN=Update-Group,OU=Groups,OU=Austria,OU=Europe,DC=contoso,DC=de'
)
$SearchBase = 'OU=Computer,OU=Berlin,OU=Germany,OU=Europe,DC=contoso,DC=de'

Get-ADComputer -SearchBase $SearchBase -Filter "Name -like '*use*'" -Properties MemberOf | 
Where-Object {Compare-Object -ReferenceObject $_.MemberOf -DifferenceObject $UpdateGroupList -IncludeEqual -ExcludeDifferent } |
Select-Object Name,@{
    Name = 'UpdateGroupList'
    Expression = {
        ($_.MemberOf |
            Get-ADGroup |
                Where-Object -Property Name -like -Value '*Update-Group*').Name -join ', '
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [January 22, 2023, 4:36pm UTC](https://forums.powershell.org/t/need-help-formatting/21432/3 "2023-01-22T16:36:16Z")

</div>

Thanks @Olaf for answering.

We actually have around 12-15 of such update groups. But all of them are in same OU.  
So do we need to mention all of it’s Distinguished Name in updategrouplist variable ?

Also we have the servers to search in 3 different OUs. For that used the @( ) definition like update group list, but was getting error as Searchbase didn’t accepted array input.

What could be done in this case ?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 22, 2023, 5:37pm UTC](https://forums.powershell.org/t/need-help-formatting/21432/4 "2023-01-22T17:37:35Z")

</div>

> [@Rahul20](#):
>
> So do we need to mention all of it’s Distinguished Name in updategrouplist variable ?

To be able to compare the reference array against the one you get with the query from your AD you have to have exact matches. So - YES - you have to provide all of them with their distinguished names - just like I already mentioned it. 🤷🏽‍♂️

> [@Rahul20](#):
>
> Searchbase didn’t accepted array input.

Either you use the OU where all of the sub OUs are saved in or you use a loop to iterate over the array of OUs. 🤷🏽‍♂️

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/need-help-formatting/21432/5 "2024-05-16T20:16:02Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


