Search code examples
powershellpowershell-5.0

wildcard inside string behaves strangely


I've just started to learn PS. Can somebody explain me below example (the reason for it if possible):

Get-Command s*rvice

gives hits like Set-Service, Start-Service, etc. but not commands like New-Service, Restart-Service, etc.


Solution

    1. You're imagining that the * represents a single character, but it can match any number of characters. s[e]rvice and s[eeeeee]rvice and s[tart-se]rvice

    2. You're reading that s*rvice will match exactly the text you type, and vary only where the wildcard is. But you're expecting it to match New-Service as if it has wildcards at the start and end like *s*rvice* - but it doesn't, it will only match the pattern you typed; the start must start with s and the end must finish with e.