a test usage of logical operators

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 10 months ago.

  • Author
    Posts
  • #22100
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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.

  • #22101
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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*

  • #22102
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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 , 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.

    • #22107
      Profile photo of Garegin Asatryan
      Garegin Asatryan
      Participant

      [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 *get*system* 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?

  • #22110
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

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

  • #22112
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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".

  • #22117
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

You must be logged in to reply to this topic.