Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 982405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:41:27+00:00 2026-05-16T04:41:27+00:00

I wanted to write a small script that searched for an exact file name,

  • 0

I wanted to write a small script that searched for an exact file name, not a string within a file name.

For instance if I search for ‘hosts’ using Explorer, I get multiple results by default. With the script I want ONLY the name I specify. I’m assuming that it’s possible?

I had only really started the script and it’s only for my personal use so it’s not important, it’s just bugging me. I have several drives so I started with 2 inputs, one to query drive letter and another to specify file name. I can search by extension, file size etc but can’t seem to pin the search down to an exact name.

Any help would be appreciated!

EDIT : Thanks to all responses. Just to update. I added one of the answers to my tiny script and it works well. All three responses worked but I could only use one ultimately, no offence to the other two. Cheers. Just to clarify, ‘npp’ is an alias for Notepad++ to open the file once found.

$drv = read-host "Choose drive"
$fname = read-host "File name"
$req = dir -Path $drv -r | Where-Object { !$PsIsContainer -and  [System.IO.Path]::GetFileNameWithoutExtension($_.Name) -eq $fname }
set-location $req.directory
npp $req
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:41:28+00:00Added an answer on May 16, 2026 at 4:41 am

    From a powershell prompt, use the gci cmdlet (alias for Get-ChildItem) and -filter option:

    gci -recurse -filter "hosts"
    

    This will return an exact match to filename “hosts“.

    SteveMustafa points out with current versions of powershell you can use the -File switch to give the following to recursively search for only files named “hosts” (and not directories or other miscellaneous file-system entities):

    gci -recurse -filter "hosts" -File 
    

    The commands may print many red error messages like “Access to the path 'C:\Windows\Prefetch' is denied.“.

    If you want to avoid the error messages then set the -ErrorAction to be silent.

    gci -recurse -filter "hosts" -File -ErrorAction SilentlyContinue
    

    An additional helper is that you can set the root to search from using -Path.
    The resulting command to search explicitly search from, for example, the root of the C drive would be

    gci -Recurse -Filter "hosts" -File -ErrorAction SilentlyContinue -Path "C:\"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.