Solved

Powershell grep -v

Posted on 2015-01-23
2
261 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 68

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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now