Excluding non-essential text from AD query

Welcome Forums General PowerShell Q&A Excluding non-essential text from AD query

Viewing 5 reply threads
  • Author
    Posts
    • #269138
      Participant
      Topics: 8
      Replies: 4
      Points: 96
      Rank: Member

      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
           }
      }

    • #269141
      Participant
      Topics: 2
      Replies: 380
      Points: 513
      Helping Hand
      Rank: Major Contributor

      Use the -ExpandProperty parameter with Select-Object.

       

    • #269147
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      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:

       

       

    • #269150
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Use the -ExpandProperty parameter with Select-Object.

      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.

    • #269165
      Participant
      Topics: 8
      Replies: 4
      Points: 96
      Rank: Member

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

    • #269183
      Participant
      Topics: 25
      Replies: 206
      Points: 772
      Helping Hand
      Rank: Major Contributor

      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)

Viewing 5 reply threads
  • You must be logged in to reply to this topic.