Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from  Instant Windows PowerShell Guide

Product type Book
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Pages 86 pages
Edition 1st Edition
Languages
Author (1):
Harshul Patel Harshul Patel
Profile icon Harshul Patel
Toc

Typing enhancements (Intermediate)


In the previous recipes, we have covered the basic changes that took place with the release of Windows PowerShell v3.0. Let's have a look at typing enhancements in the Version 3 console.

We have tab completion for CMDLETs in each version of Windows PowerShell, especially in Version 3.0 where we have tab completion for parameter values as well.

Getting ready

We have some simplified syntax introduced in the latest version of Windows PowerShell with respect to the Where-Object and ForEach-Object CMDLETs.

How to do it...

  1. In Version 2.0, the following command retrieves a list of running processes, which have a handles count greater than 1000 from the local machine:

    PS C :\> Get-Process | Where-Object {$_.Handles -gt 1000}
    
  2. In Version 3.0, the following command does the same operation as the previous command statement:

    PS C :\> Get-Process | Where-Object Handles -gt 1000
    

    Let's check use of ForEach-Object and Where-Object by using the following points:

    • The following command statement lists down only files and directory names from the C:\Scripts location:

      PS C :\> Get-ChildItem C:\Scripts | ForEach-Object Name
      
    • The following command retrieves the list of running services on the local computer which have the win keyword in their names:

      PS C :\> Get-Service | Where-Object {$PSItem.Status -eq "Running" -and $PSItem.Name -like "*win*"}
      

How it works...

If we compare the preceding two different version's outputs, it is evident that PowerShell v3.0 has simplified syntax. Moreover, we do not need to use curly braces anymore to run a command statement.

Also, it automatically gets the previous command pipeline output as input for the Where clause. We don't need to explicitly provide the parameter value with the $_ syntax.

Novice users would find the $_ syntax a bit strange; now, in PowerShell v3.0, we can use $PSItem instead of $_.

Tip

It is recommended to use full syntax with curly braces and $PSItem when we draft a script.

The same is the case with Where-Object; we don't need to use curly braces and the $_ syntax if we are dealing with ForEach-Object in PowerShell v3.0.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013 Publisher: Packt ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}