[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell grep -v

Posted on 2015-01-23
2
Medium Priority
?
417 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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