a test usage of logical operators

I want to play around with logical operators

How can I say get-command get -and win. Which would retrieve commands that have both get an win in their names.

It depends on the command. Some commands will let you do that sort of thing (such as the -Filter parameter on the Active Directory cmdlets), but for the most part, you can’t do complex patterns like that without resorting to Where-Object. This is inherently slower, since Get-Command would be giving you all commands and then your script would be filtering them, instead of letting Get-Command perform its own faster filtering in the compiled .NET code.

In this case, however, since Get is a verb and Win is part of the noun, you could probably get away with this: Get-Command Get-Win

Very few commands would accept that kind of syntax.

In general, you’d have to get more commands than you wanted, and then filter them.

Get-Command | Where-Object { $_.Name -like '*win*' -and $_.Name -like '*get*' }

In the specific case of Get-Command, read the help. Notice that the -Name parameter accepts values of type <string>, meaning “more than one string.”

Get-Command -Name '*get*','*win*'

That would be an “or” logical operator, not an “and.” Get-Command doesn’t have a way of providing two strings and only returning results that match both strings. Other commands might differ in their capabilities. For example, Get-ADUser has a -Filter parameter that can accept Boolean operators like -and and -or. The AD commands are the only ones that immediately spring to mind which do that.

[blockquote]In this case, however, since Get is a verb and Win is part of the noun, you could probably get away with this: Get-Command Get-Win[/blockquote]

hmm. It would actually work even if it wasn’t

gcm getsystem* does the job too.

However for some reason I can’t get regex-es to work. for example gcm ‘i.al’ gives an error. Have any ideas why?

Get-Command uses wildcards that you would use with the -like operator, rather than a regular expression (so ‘i?al’ should work, as it’s essentially the same effect as ‘i.al’ in a regex). Regex is generally limited to operators (-match, -split, -replace) and the Select-String cmdlet. I can’t think of any other cmdlet off the top of my head which accepts a regular expression for one of its parameters, but lots of them support basic wildcards.

getsystem* works fine so long as the word “get” comes before “system”, which it always should in a PowerShell command.

thanks for the help. I want to emphasize again that a majority of my posts are academic in nature. There are silly and stupid, but are given to learn about different concepts in powershell (and scripting languages in general). They aren’t aimed at solving “real-life problems”.

No worries. My questions tend to be exactly the same. :slight_smile: I probably missed my calling as a QA tester.