?
Solved

Powershell Regular Expression script question

Posted on 2011-02-18
3
Medium Priority
?
573 Views
Last Modified: 2012-06-27
I am trying to write a script to read in a test file, search for just the user and return that.

SOMEDOMAIN\SomeUser             Full Control[aasdfa]
SOMEDOMAIN\SomeUser             Partial Control[aasdfa]
SOMEDOMAIN\SomeUser             Full Control[aasdfa]

I have some of the code needed and I also have the a regex statement that works.    I am getting caught on the output of $Matches
When I run the code and check $matches is only contains the last item found/returned.

I know I am missing something simple, any insight would be appreciated :)



Get-Content file.txt |where-object{$_ -match '\S*\s'}

Open in new window

0
Comment
Question by:fertigj
[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
3 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 34932277
This executes with an implicit loop:
Get-Content file.txt |where-object{$_ -match '\S*\s'}

Open in new window

Since Get-Content returns an array, all you'll have in $Matches is the last value. If you need to parse Matches it would be wies to execute this as an explicit ForEach-Object loop:
Get-Content file.txt | ForEach-Object {
  If ($_ -match '\S*\s') {
    # Do things with $Matches
  }
}

Open in new window

HTH

Chris
0
 
LVL 1

Author Comment

by:fertigj
ID: 34957650
ok.   I am sure I am missing something simple, but what if I just want to write this to the host  ie   write-host $matches.value.


When I do this I get nothing back.   Thoughts?
0
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 1000 total points
ID: 34958885

$Matches is a HashTable, it doesn't have a property explicitly called "Value".

I think you want:

$Matches[0]

Which returns the first match, at Key 0.

Chris
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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 this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 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