Thursday, August 25, 2011

Wildcards in PowerShell

 

Hi Guys,

Before start learning PowerShell first we need to clear or learn some basic logics and basic  steps of scripting, that’s why I decide to learn about wildcards first. So Lets Start

Question: what are wildcards?

Answer: A wildcard character can be used to substitute for any other character or characters in a string.

Question: Which wildcards are available in PowerShell and what are there meaning?

Answer: The wildcards are available in PowerShell are * ? and []

Wildcard Description
* matches Zero or More characters
? match Exactly one character
[a-z] matches a range of characters
[abc] matches specified character

 

Examples

Wildcard *

Lets Run the following command: Dir * it shows all file and folders in current Directory 

 wildcards-1

Let run another command: Dir *.* it will show all files in current directory. *.* means all file with any file extension

wildcards-2

Lets search for all .mp3 files in our current directory.: Dir *.mp3

wildcards-3

Now lets search for all file and folders which starts with B: Dir b* 

Capture4

-----------------------------------------------------------------------------------------------

Wildcard ?

Now let try our another wildcard character ? , in this example we are searching for a .txt file which names end with at and we forget what  is the name: dir ?at.txt   this command return all file which has at.txt at the end.

wildcards-5

Let See the another Example, in this example i am looking for a txt whose name is 3 character long. Dir ???.txt

wildcards-6

Now lets join our both wildcards and search for all .mp3 and .mp4 available in our current directory: dir *.mp?

wildcards-7

-----------------------------------------------------------------------------------------------

Wildcard [] matches specify range of characters

In [] wildcard we can specify a range of characters to match , for example if we specify [a-d] then it means all alphabets between A-D (A,B,C,D) or if we specified [1-8] then it means match all digits between 1-8 (1,2,3,4,5,6,7,8)

Dir: [a-z]at.txt  this command check for all file start with a-z character and at.txt at the end

wildcards-8

In another example lets search for all folder starts whose name start from 1-6

Dir [1-6]

wildcards-9

-----------------------------------------------------------------------------------------------

Wildcard [] matches specify characters

In this wildcard rather then provide a range we specify which characters we want to match. for example we want to match character start with b,c,d then we can specify it as [acd] then it matched only files or folders starts with A,C,D and ignore everything else.

For example: dir [bcl]ook.txt , this command look for all files start with B,C,L and have ook.txt after it

wildcards-10

-----------------------------------------------------------------------------------------------

 

I hope this article helps you to understand Wildcards a little bit :)

Thanks for reading it

Regards

Aman Dhally

 

Note: Dir command is an alias of Get-Childtem in PowerShell

1 comment:

Note: Only a member of this blog may post a comment.