POSH Concept : Understanding WildCards

The word wildcard is quite intriguing and potentially comes from the world of Games and gambling where most of the results are unknown or unpredictable. In the game of cards, there are few cards termed as ‘Jokers’ and they represent ‘WildCard’ meaning that they can assume any value. They are amorphous and can assume values depending on the context being used.

Wildcard in programming world is typically used to find and search sequence of characters within a text for which it is not sure or known whether the search sequence of characters exists in target text, if the sequence of characters exists, then how many? Also, It might not be a single exact sequence of characters to find. There could be multiple variations of sequence of characters that are to be found – all at one time. Some of the characters to be searched might not even be known at all while searching.

Wildcards helps in finding solutions to all the above mentioned situations. It helps in searching sequence of characters is almost fuzzy manner.

Every programming language designates few characters in its lexicon as Wildcard characters depending on its grammar. Powershell has its grammar and also supports Wildcard characters. Based on its grammar, Powershell has the following characters that can be used as wildcard characters.

  • ?
  • *
  • []
  • [-]

‘?’ means ‘any single character can be replaced with it’. ‘?’ signifies that any one character is a valid character wherever it appears. For example: in characters ‘W3w?’, ‘?’ can be replaced with any legitimate character. W3wa, w3wp, w3wz, w3w0 would match to the above mentioned wildcard pattern.

‘*’ means ‘zero or more character can be replaced with it’. ‘*’ signifies that any number and any character is a valid to replace wherever it appears. For example: in characters ‘W3w*’, ‘*’ can be replaced with any legitimate character. W3wa, w3wpi, w3wz123 would match to the above mentioned wildcard pattern.

‘[]’ means ‘any one of the specified characters within these brackets can be replaced with it’. ‘[]’ signifies that any one character is a valid to replace wherever it appears. For example: in characters ‘W3w[mnopq]’, ‘[]’ can be replaced with any legitimate character ‘m’, ‘n’, ‘o’, ‘p’, ‘q’. W3wm, w3wn, w3wo, w3wp, w3wq would match to the above mentioned wildcard pattern.

‘[-]’ means ‘any one of the specified characters within the range available in the brackets can be replaced with it’. ‘[-]’ signifies that any one character is a valid to replace wherever it appears from the range. For example: in characters ‘W3w[m-q]’, ‘[-]’ can be replaced with any legitimate character ‘m’, ‘n’, ‘o’, ‘p’, ‘q’. W3wm, w3wn, w3wo, w3wp, w3wq would match to the above mentioned wildcard pattern.

one of the important character in the Powershell grammar for wildcards is the ‘`’ character. This acts as an escape character and is of immense importance while searching for strings that might contain one of the wildcard characters. for example if the search characters include ‘*’ to be searched verbatim, there is no other way to let Powershell know that ‘*’ is not a wildcard character without the usage of ‘`’. ‘`*’ would negate the ‘*’ being as wildcard character and would be searched as a normal character.

One important thing to remember is that not all properties belonging to cmdlets are wildcard savvy meaning that only some properties can be used along with wildcards and this information about which properties can be used for searching using wildcards is available as part of cmdlet parameters description on MSDN.

Lets understand the usage of Wildcards in powershell using example..

We will use a simple cmdlet ‘Get-Service’ for all the examples. Get-Service cmdlet when run, returns all Windows Services available within the operating system.

Example 1 : find all services that start with letters ‘win’

Get-Service -name ‘win*’
The result is all services whose name starts with ‘win’ on my machine

  • WinDefend
  • WinMgmt
  • WinRM
Example 1 : find all services that ends with letters ‘win’

Get-Service -name ‘*svc’
The result is all services whose name ends with ‘svc’ on my machine

  • W3SVC
  • Wcmsvc
  • wcncsvc
  • WdNisSvc
  • Wecsvc
  • WEPHOSTSVC
  • WerSvc
  • WinHttpAutoProxySvc
  • WlanSvc
  • wlidsvc
  • WMPNetworkSvc
  • workfolderssvc
  • WPCSvc
  • wscsvc
  • wudfsvc
  • WwanSvc

I will post some more examples in future posts!! stay tuned..

Cheers!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s