Solved

Powershell grep -v

Posted on 2015-01-23
2
289 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
  • 2
2 Comments
 
LVL 69

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 69

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Set OWA language and time zone in Exchange for individuals, all users or per database.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

778 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