EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Wildcards
 

PowerShell Wildcards

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

PowerShell Wildcards

 

 

Introduction to PowerShell Wildcards

Wildcards are patterns that are used to match multiple characters. They are used to specify certain patterns in the cmdlets to filter the results or return only the results that matches the wild card specified. There are four types of wild cards that are available in PowerShell. They are represented as *, ?, [m-n] and [abc]. More than one wildcard pattern can be used together. Wildcard parameters are accepted by many cmdlets in PowerShell. Cmdlets that accepts wild card characters as input are case insensitive. Wildcards can also be used in a cmdlet to filter properties that are available in the cmdlet results. This article will cover in detail about wildcards, their types, their usage along with various examples.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Types of Wildcard in PowerShell

There are four types of wildcards that are available in PowerShell.

Syntax:

1) *: It denotes that the matching pattern must be zero or more characters. If only * is used, then all the matching characters are returned.

Example: Ba* it will return bagubali, batcha, basel. It won’t return like America, Africa.

2) ?: It denotes that a character at the position must be matched.

Example: ?b will return ab, cb,db and won’t return aa,ac,aj.

3) []: This denotes that a pattern within the range must be checked for matching.

Example: [a-c]de will return ade, bde and won’t match with zde.

4) []: It matches only the specific characters mentioned in the range.

Example: [ab]tef will match with atef and btef.

Examples to Implement PowerShell Wildcards

Below are the examples of PowerShell Wildcards:

Example #1

Code:

Write-Host "Welcome to wild card in powershell example"
Write-Host "Welcome to wild card in powershell example"
Write-Host "welcome to * wild card demo"
Write-Host "listing all txt files inside a folder"
Get-ChildItem C:\Users\EDUCBA\*.txt
write-host "Filtering services running on the machine"
Get-Service | Where-Object {$_.Name -like "net*"}
Write-Host "listing all excel files inside a folder"
Get-ChildItem C:\Users\EDUCBA\*.xlsx
Write-Host "Recurrsing through a sub folders and finding all text files"
Get-ChildItem -Path C:\\Users\EDUCBA\*.txt -Recurse -Force
Write-Host "Recurrsing through a sub folders and finding all excel files"
Get-ChildItem -Path C:\\Users\EDUCBA\*.xlsx -Recurse -Force

Output:

Powershell Wildcards Ex 1

Example #2

Code:

Write-Host "Welcome to wild card in powershell example"
Write-Host "welcome to ? wild card demo"
Write-Host "listing services"
Get-Service | Where-Object {$_.Name -like "???logon"}
write-host "Filtering services running on the machine"
Get-Service | Where-Object {$_.Name -like "??logon"}
write-host "listing services running on the machine"
Get-Service | Where-Object {$_.Name -like "?logon"}
Write-Host "Recurrsing through a sub folders and finding all text files"
Get-ChildItem -Path C:\\Users\EDUCBA\*.txt -Recurse -Force
Write-Host "Recurrsing through a sub folders and finding all excel files"
Get-ChildItem -Path C:\\Users\EDUCBA\*.xlsx -Recurse -Force

Output:

Powershell Wildcards Ex 2

Example #3

Code:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of [] wild card pattern"
$input=@("one","two","three","four","five","oneone","onetwo","onethree","aone","bone","cone")
Write-Host "Word contains one"
$input -like "[a-z]one"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains abone"
$input -like "[ab]one"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]one"

Output:

Powershell Wildcards Ex 3

Example #4

Code:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of match specific characters wild card pattern"
$input=@("one","two","three","four","five","oneone","onetwo","onethree","aone","bone","cone")
Write-Host "Word contains one"
$input -like "[o]ne"
$input=@("abone","two","three","afour","five","aboneone","onetwo","onethree","aone","bone","abonethree","cfour","bfour","dfour")
Write-Host "word contains four"
$input -like "[abcd]four"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]one"

Output:

Powershell Wildcards Ex 4

Example #5

Code:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of multiple wild card pattern"
Write-Host "Applying multiple wild card pattern in list of services"
Get-Service | Where-Object {$_.Name -like "*n?t*"}
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "[a-z][a-d]*"
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "*ne[s-z]?o*"
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "*ne[s-z]?"

Output:

Powershell Wildcards Ex 5

Example #6

Code:

write-host "Demo of wild card operator"
Write-Host "Match with wildcard charcaters for a single string"
$input="My name is ramesh"
$input -match "na*"
Write-Host "Match without wildcard charcaters for a single string"
$input -match "na"
Write-Host "Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab*"
Write-Host "Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab"
Write-Host "Not Match with wildcard charcaters for a single string"
$input="My name is sacinn tendulkar"
$input -notmatch "za*?"
Write-Host "Not Match without wildcard charcaters for a single string"
$input -notmatch "za"
Write-Host "Not Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za[-z]*"
Write-Host "Not Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za[u]"

Output:

Example 6

Example #7

Code:

Write-Host "Example of Wild card usage in PowerShell"
Write-Host "demo of match specific characters wild card pattern"
$input=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "Word contains test"
$input -like "[t]est*"
$input=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "word contains test1"
$input -like "test*"
$input=@("test","1test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "word contains multiple filters"
$input -like "?test1"
Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of multiple wild card pattern"
Write-Host "Applying multiple wild card pattern in list of services"
Get-Service | Where-Object {$_.Name -like "*n?t*"}
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "[a-z][a-d]*"
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "*ne[s-z]?o*"
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "*ne[s-z]?"

Output:

Example 7

Example #8

Code:

Write-Host "Example of wild card characters"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "test*"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "dfsdf?*"
Write-Host "Demo of multiple check values wild card"
$input="one","two","three","four","five"
$input -contains "one?","two*"
$input="one","two"
$input, "three","four" -ccontains $input
$input= "sdfsdfdsf","adsadsad","dfdf","sdf"
$input -notcontains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -notcontains "test"
$input="one","two","three","four","five"
$input -notcontains "one","two"
$input="one","two"
$input, "three","four" -notcontains $input

Output:

Output 8

Example #9

Code:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of [] wild card pattern"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "country staring with a"
$input -like "a*"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "word contains be"
$input -like "be*"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]*"

Output:

Output 9

Conclusion

Thus, the article covered in detail about wild card expressions in PowerShell. It also explained about the types of wild card expressions that are available and their usage with appropriate examples. The article explained in detail about each wild card expression type with corresponding sample programs. Using Multiple wild card expressions together is also explained in detail along with an appropriate demo program. To learn more in detail, it is advisable to write and practice sample programs.

Recommended Articles

This is a guide to PowerShell Wildcards. Here we discuss the different types of Wildcard in PowerShell and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

  1. Different Commands of PowerShell Scheduled Task
  2. Examples of PowerShell Convert to String
  3. PowerShell If-Not | Examples
  4. Guide to PowerShell Rename Folder
  5. PowerShell Send Mail | Examples
  6. PowerShell Get-Service
  7. Guide to PowerShell Continue

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW