Attention: This website is optimized for Internet Explorer 8+ and all other current browsers.
Viewing this site in your current browser may result in formatting issues. We suggest you update your browser Here. Thanks!
Contact OrcsWeb Managed Windows Hosting Call us: 1-888-313-9421

Follow Us

OrcsWeb Hosting
  • Facebook
  • Google
  • Linkedin
  • Twitter
  • Home
  • Managed Hosting
    • Windows Cloud Servers
    • Dedicated Windows Servers
    • Windows Hybrid Hosting
    • Managed Shared Hosting
    • PCI Compliant Hosting Solutions
    • Managed Services
      • Managed Windows Hosting
      • Standard Managed
      • Complete Care
      • The Migration to Orcsweb
    • Our Data Center
  • Solutions For
    • SaaS Hosting Solutions
    • eCommerce Hosting
    • Web Apps, Blogs, CMS, Web 2.0 Hosting
    • Hosting for Rich Media Sites
    • Managed Private Cloud Hosting
  • Why OrcsWeb
    • Why OrcsWeb
    • About OrcsWeb
    • How We’re Different
    • Meet some of our clients
    • Check Out Our Press Releases
  • Communicate with Us
    • Get a Quote
    • Contact Us
    • The Orcs Web Blog
    • e-Newsletter Signup
    • Client Login
    • Knowledge Base
    • Support Request
    • Careers At OrcsWeb
    • Partner with OrcsWeb
      • Partner Login
      • Affiliate Program
  • Blog
Search

PowerShell Pearl: Filter by Contained Text

0

I have just recently started using PowerShell. While this blog will not be where you want to go to learn PowerShell, as I pick up little pearls here and there, I will try to share them with small samples and quick PowerShell scripts. I am no PowerShell expert, so if you find any errors, please let me know. 

Today's Pearl: 

If you are returning a set of results and you want to filter those results by text contained in one of the fields there are two ways I found you can do this. The first is using a Field.Contains(“search text”) –eq “true” and the other (thanks Scott) is doing a Field –match “search text”. 

So if you wanted to see all of the System Event logs that have cmd.exe is the message you could get this using either of these methods: 

Get-EventLog system | where { $_.Message.Contains("cmd.exe") -eq "true" }

or…

Get-EventLog system | where { $_.Message -match "cmd.exe" }

Either of these can be used in the negative form just as easily:

Get-EventLog system | where { $_.Message.Contains("cmd.exe") -eq "false" }

Get-EventLog system | where { $_.Message -notmatch "cmd.exe" }

That’s it! The –match and –notmatch are probably the easier of the two to use, although I am sure there is a reason for each of them that I am not aware of. Hopefully things like this will come to be part of my knowledge as I know more.

Update:

Okay, I just learned that -match and -nomatch are regular expression comparison operators. Another set comparison operators you could use are -like and -notlike. These are the wildcard comparison operators. It could be used like so:

Get-EventLog system | where { $_.Message -like "*cmd.exe*" }

Be Sociable, Share!
    • Tweet
    PowerShell Tips

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


    Blog Archives


    Recent Posts

    • Deleting file when getting “Cannot delete file: Cannot read from the source or disk”
    • How to Calculate IOPs on a Windows Cloud Server
    • MySQL Workbench Quick Overview

    Subscribe

    RSS Feed
    You can subscribe to the OrcsWeb Blog feed via RSS to receive updates when new entries are posted.

    Explore Our Windows Hosting Solutions


    Windows Cloud Server Hosting
    Dedicated Windows Server Hosting
    Windows Server Hybrid Hosting


    Tags

    pci-compliance sql managed windows hosting windows-cloud-servers server cloud windows cloud server hosting windows IIS8 Windows Cloud Hosting Pci compliant hosting asp-net cloud-server Windows Cloud Server hosting sql server Windows Cloud SSL Security FTP iis windows-server PCI-DSS managed hosting cloud-hosting IIS 7 dedicated-server Privacy How To PowerShell database Windows Server 2012 Technical Troubleshoot saas hosting Servers ecommerce iis7 cloud-servers dedicated
    Windows Server hosting Certifications

    Why OrcsWeb?

    "I cannot think of anything that OrcsWeb needs to improve upon. Every encounter with an OrcsWeb staff member has been great. They are knowledgeable, pleasant and eager to help. The organization is comprised of outstanding professionals."
    — Pete Peterson, SCI Healthcare Group

    At OrcsWeb, we take a completely unique approach from all other hosting providers. We implement over 17 years of architectural expertise right from the start, and follow up by serving our clients as a true extension of their internal team, identifying and resolving issues before they arise. Read more →
    (c) 1996 - 2013 OrcsWeb
    • About OrcsWeb
    • Privacy Policy
    • Windows Cloud Servers
    • Dedicated Windows Servers
    • Shared Hosting