Now Available:

Featured Resource:

line

Newsletter

Email Address:


line

Ask the Expert

Have a question for our resident expert? Email your questions to Greg.

« Realtime Windows Server "Help an Admin Out" Contest Winner #7 - Boris! | Main | MMS 2008 Registration is Now Open »

Don Jones’ Favorite PowerShell Cmdlets, Part 3 of 10: Where-Object

Get-Process. Get-Service. Heck, especially Get-EventLog. What do they all have in common? They tend to return too much information. Fortunately, they’re not just displaying plain text, they’re displaying objects, so all we need to do is filter those objects a bit, using Where-Object. Pipe in some objects and specify a criteria, and Where-Object removes all the objects that don’t meet your criteria:

Get-WMIObject Win32_Service | Where { $_.StartMode -eq “Auto” }

“Where” is an alias to Where-Object, and it’s a lot easier to type - and I think it makes the command easier to read, too. $_, used in the criteria, represents “those objects that were piped in.” $_.StartMode, then, refers to “the StartMode property of those objects that were piped in.” Pipe objects to Get-Member (its alias is gm) to see their properties:

Get-WMIObject Win32_Service | gm

I use this with event log entries all the time, to search for a specific event ID:

Click past the fold for more…

Get-EventLog System -newest 100 | Where { $_.EventID -eq 1071 }

And so forth - very convenient. Where-Object is one of the core PowerShell cmdlets, so you can use it with Exchange Server 2007, System Center Virtual Machine Manager, and anything else “built on” PowerShell. Imagine getting all mailboxes, and filtering for just those over a certain size… you could then pipe those large mailboxes to Move-Mailbox, moving them off to a new, larger mailbox server. Cool stuff - and an amazing one-line command!

Don Jones is the Lead Scripting Guru for SAPIEN Technologies and a Windows PowerShell MVP. He’s the co-author of Windows PowerShell: TFM, 2nd Edition and is the Windows PowerShell columnist for TechNet Magazine. Don is also the Director for the new centralized, independent PowerShell online community: www.PowerShellCommunity.org.

Get your hands on the full Don Jones Favorite Cmdlets Series at this address:
http://www.realtime-windowsserver.com/type/mt-search.cgi?tag=don%20jones%20favorite%20cmdlets&blog_id=1

TrackBack

TrackBack URL for this entry:
http://www.realtime-windowsserver.com/type/mt-tb.cgi/514

Post a comment

(All comments are approved by site leader before appearing here. Thanks for commenting!)

line

Greg Shields' Bio:

Greg Shields, is an independent author, instructor, and IT consultant based in Denver, Colorado, and a co-founder of Concentrated Technology. With nearly 15 years of experience in information technology, Greg has developed extensive experience in systems administration, engineering, and architecture specializing in Microsoft systems management, remote application, and virtualization technologies. Greg is a Contributing Editor for Redmond Magazine, MCPmag.com, and Virtualization Review Magazine and is the author of five books, including Windows Server 2008:  What’s New / What’s Changed. Greg is also a highly sought-after instructor and speaker, speaking regularly at conferences like TechMentor Events, and producing computer-based training curriculum for CBT Nuggets.  Greg is a recipient of Microsoft "Most Valuable Professional" award with a specialization in Windows Terminal Services.