Solved

Powershell Regular Expression script question

Posted on 2011-02-18
3
544 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
  • 2
3 Comments
 
LVL 70

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 70

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

18 Experts available now in Live!

Get 1:1 Help Now