Powershell Command for searching & finding 2 files from a diectory

Welcome Forums General PowerShell Q&A Powershell Command for searching & finding 2 files from a diectory

  • This topic has 10 replies, 5 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 10 reply threads
  • Author
    Posts
    • #223671
      Participant
      Topics: 2
      Replies: 3
      Points: 25
      Rank: Member

      Hi,

      I am looking for the shortest PowerShell command to search and display 2 files in my home directory. However, the command I am using is not technically right. I am looking for a better powershell command.

      Here is my problem.

      I have two files called “Letter to Mom” & “Letter to Dad” in my Home Directory inside a folder called “myletter”

      C:\Users\Apple>  is my home directory.

      I used the following command Get-ChildItem -Filter Letter* -Recurse

      However, this is not right because it will list all files starting with “Letter”.

      Can anyone provide a better ‘smallest’ command which only lists the “Letter to Mom” & “Letter to Dad” files

       

      Any input will be appreciated

    • #223686
      Participant
      Topics: 20
      Replies: 30
      Points: 70
      Rank: Member

      What about Get-ChildItem -Filter “Letter to*” -Recurse

    • #223878
      Participant
      Topics: 2
      Replies: 3
      Points: 25
      Rank: Member

      Any other options?

    • #223896
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      What’s wrong with it? It’s pretty short. To make sure not to catch the “letter to parents” 😉 you have to make it more complex …

      Get-ChildItem -Path 'D:\sample' -Filter 'Letter to*' -Recurse |
          Where-Object -Property BaseName -Match -Value 'mom|dad'
    • #223929
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      Why recurse? You know where they exist.

      gci "$env:HOMEPATH\myletter" "letter to *"

    • #223992
      Participant
      Topics: 2
      Replies: 3
      Points: 25
      Rank: Member

      Thanks everyone

    • #224340
      Participant
      Topics: 2
      Replies: 3
      Points: 25
      Rank: Member

      Hi,

      There is another problem.

      If we add another file called letter to grandfather, it should not be there.

      I mean if we write the

      gci "$env:HOMEPATH\myletter" "letter to *"

      SO I THINK THERE SHOULD BE SOMETHING WITH MUM|DAD
      can you solve?
    • #224364
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      What’s wrong with it? It’s pretty short. To make sure not to catch the “letter to parents” 😉 you have to make it more complex …

      Olaf already did.

      
      Get-ChildItem -Path 'D:\sample' -Filter 'Letter to*' -Recurse |
      Where-Object -Property BaseName -Match -Value 'mom|dad'
      
      
    • #224367
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      Here is another way to only get mom and dad.

      
      gci -include "*dad*","*mom*" "letter to *" -recurse
      
      
    • #224439
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      …. can you solve?

      Powershell.org is not a free code writing service. What’s the actual problem you’re trying to solve? We won’t write Powershell code piece by piece for you.

    • #224538
      Participant
      Topics: 6
      Replies: 16
      Points: 47
      Rank: Member

      Get-ChildItem -Filter Letter* -Recurse | where {$_.name -match 'mom|dad'} | select name
      It’s pretty much the same context as posted by Olaf.

Viewing 10 reply threads
  • You must be logged in to reply to this topic.