Solved

Powershell grep -v

Posted on 2015-01-23
2
325 Views
Last Modified: 2015-01-23
Still pretty new to powershell. I am looking to take the results of:

Get-ADPrincipalGroupMembership <user> | Select -Expand Name | Foreach-Object {$_.trim()}

and remove some of the Names that come back. On linux, I could grep -v for say Group1. I am not sure how to remove results from the list. There will be multiple results that I want to remove. So, for this example, lets use Group1 and Group2. Is there a way to do a powershell version of grep -v?
0
Comment
Question by:stakor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40566293
Since we are not operating with string results but objects in PowerShell, you usually need to do it different. However, you've done string expansion here, so one way is
Get-ADPrincipalGroupMembership <user> |
  Select -Expand Name |
  Foreach-Object {$_.trim()} |
  Where-Object { 'Name1', 'Name2' -notcontains $_ }

Open in new window

With PS 3 and above, we can use the more straightforward -IN:
Get-ADPrincipalGroupMembership <user> |
  Select -Expand Name |
  Foreach-Object {$_.trim()} |
  Where-Object { $_ -notin 'Name1', 'Name2' }

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40566306
grep would translate to Select-String, if like to stay with something more similar:
Get-ADPrincipalGroupMembership <user> |
  Select -Expand Name |
  Foreach-Object {$_.trim()} |
  Select-String 'Name1', 'Name2' -NotMatch -SimpleMatch

Open in new window

This is more like fgrep -v because of -SimpleMatch, which stops using RegEx. Be aware that you would match any part of the name with that (as you do with *grep).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question