Solved

Powershell Regular Expression script question

Posted on 2011-02-18
3
569 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 250 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 250 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

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).
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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