Solved

Powershell grep -v

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete Folder Containing Only Specified File Types 4 32
sccm client without collection 1 37
problem with loop 3 48
Optimize find and replace on 194 MB text file 20 41
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

8 Experts available now in Live!

Get 1:1 Help Now