FUNctions: Episode II, Attack of the File Search

Have you ever ran a “Get-ChildItem” line and run into this error?

“The specified path, file name, or both are too long” Ugh, bummer.

Obviously, as the error states, we’ve exceeded the 260 character limits. End users love to nest folders, and they love to use long directory names. For the sake of organization, I don’t blame them. We’ve gotta’ find a way to coexist, and here’s a solution.

Here’s the scenario: Somehow…. the wrong file got copied to hundreds of directories. (Don’t look at me! I didn’t do it!), and now it needs removed. Use your imagination on what the file is and why it needs removed, but we got to get it out of there ASAP!

This is where my SearchAndDestroy-File comes into play! You can grab the function here, or continue below for some pro-tips.

Section 1 | One way, I’m gonna’ find ya’!

function SearchAndDestroy-File {
param(
    [string]$folderPath,
    [string]$fileName
)

New-PSDrive "F" -PSProvider FileSystem -Root "$($folderPath)"
$files = Get-ChildItem -LiteralPath "F:\" -File "$($fileName)" -Recurse
foreach($file in $files){
    Remove-Item $file -Force -Verbose
}

Remove-PSDrive "F"

}

Here’s the workflow broken down.

  1. Create a new drive called “F:” based on the rooted folder path. ( -folderPath)
    • Let’s imagine its X:\Some Folder Name\Some Other Folder Name\ Etcetera\ Etcetera 2\
    • This is going to let us cut down on some characters by converting the folder path into a lettered drive
  2. Search for the desired file
    • A simple Get-ChildItem -Recurse will get the job done (-fileName)
  3. From an array of all of the found items, cycle through those with a foreach loop.
  4. Delete the file
  5. When done with the foreach loop, kill the F: drive.

Load this function, then use it as so:

SearchAndDestroy-File -folderPath “X:\Some Folder Name\ -fileName “Some File.pdf”

Quick, simple, dirty. I added the -Verbose, so that it will write to the host shell and you can monitor progress.

Section 2 | I’m gonna’ get ya, get ya, get ya, get ya

Now there are some gotcha’s. Listen up!

  1. Run this with PowerShell 7!
    • PowerShell 7 supports long file paths and uses .Net APIs. Its also cross platform!
  2. If you still encounter issues try making the following regkey to support long file paths in Windows PowerShell 5.1 and for legacy Windows versions.
    • Path: HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem\
    • Name: LongPathsEnabled
    • Type: DWORD
    • Value: 1

Section 3 | Are you even reading these???

Did you check out the last blog post? No? Here is it: Episode I, The Phantom Module. In this post, we discuss what functions are, but most importantly, your very own PowerShell module. Check out Section 2 of last week’s blog, then come back and pop this into your custom PowerShell module.

As always, thanks for reading! And remember, kids:

This project is provided “as is” without any warranty of any kind, express or implied. Use it at your own risk. The authors and contributors are not responsible for any damage, data loss, or other issues that may arise from using this software. You are solely responsible for any actions taken based on this code.