EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
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.

ADVERTISEMENT
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
ADVERTISEMENT
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW