# Excluding non-essential text from AD query

**URL:** <https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181>\
**Category:** PowerShell Help\
**Created:** [November 4, 2020, 6:28am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181 "2020-11-04T06:28:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zcaa0g](https://avatars.discourse-cdn.com/v4/letter/z/ecc23a/32.png) [@zcaa0g](https://forums.powershell.org/u/zcaa0g)\
**Post date:** [November 4, 2020, 6:28am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/1 "2020-11-04T06:28:06Z")

</div>

Hello,

When I run the following cmdlet:

**Get-ADComputer -Filter \* -SearchBase “CN=Computers, DC=companyname, DC=net” | Select Name**

I get the following result where it just shows the computer name as desired:

**acomputername**

However, if I use the script below, I get this format instead:

**@{Name=acomputername}**

So how do I go about excluding the **@{Name=}** portion from the computer name?

Sorry for the formatting, I wasn’t sure where to put the code in the **Tools-\>\<\> Source Code** option in relation to the \<P\>s.

**$test = Get-ADComputer -Filter \* -SearchBase “CN=Computers, DC=companyname, DC=net” | Select Name**

**foreach($line in $test) {**  
 **if($line -match $regex){**  
 **write-host $line**  
 **}**  
**}**

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [November 4, 2020, 6:44am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/2 "2020-11-04T06:44:45Z")

</div>

Use the -ExpandProperty parameter with Select-Object.

&nbsp;

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [November 4, 2020, 6:50am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/3 "2020-11-04T06:50:06Z")

</div>

When you run commands in PowerShell like Get-ADComputer it returns objects. While a text representation of those objects are displayed on the screen, that is just a representation not the full object. In your first case you piped the objects to Select-Object and specifically asked for the Name property of the object although there are many other properties available. Instead of piping to Select-Object, pipe the output to Get-Member and you can see all the available properties for the objects it returns. I suspect you want to filter the results based on the name property and a regular expression. In your foreach loop however, you are comparing the entire object (even though at this point it only has a name property since that is what you selected) against the regular expression. You would need to de-reference the object with the dot operator ($line.name -match $regex). Just to make things a little cleaner, I would write it this way:

```
Get-ADComputer -Filter * -SearchBase “CN=Computers, DC=companyname, DC=net” |
    Where-Object {$_.name -match $regex} |
        Select-Object -ExpandProperty Name
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [November 4, 2020, 6:53am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/4 "2020-11-04T06:53:48Z")

</div>

[quote quote=269141]Use the -ExpandProperty parameter with Select-Object.

[/quote]  
Looks like Matt beat me to the punch. ExpandProperty will work as because instead of returning a custom object with a Name property that you would have to reference with the property de-reference operator (dot), it will return just the string value of that property.

---

<div class="post-metadata">

**Author:** ![zcaa0g](https://avatars.discourse-cdn.com/v4/letter/z/ecc23a/32.png) [@zcaa0g](https://forums.powershell.org/u/zcaa0g)\
**Post date:** [November 4, 2020, 7:41am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/5 "2020-11-04T07:41:21Z")

</div>

Thanks for the responses from all of you. That addressed my question perfectly.

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [November 4, 2020, 10:47am UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/6 "2020-11-04T10:47:54Z")

</div>

the other approach depending on what you’re doing with the data in the end, or if you need to use multiple values out of the results.  
$line.name

or if you are trying to use it in some strings you can do $($line.name)

---

<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:29pm UTC](https://forums.powershell.org/t/excluding-non-essential-text-from-ad-query/15181/7 "2024-05-16T20:29:10Z")

</div>


