Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active February 19, 2026 07:37
Show Gist options
  • Select an option

  • Save scriptingstudio/728d98cb55ebaf1917cea3e41939b1fb to your computer and use it in GitHub Desktop.

Select an option

Save scriptingstudio/728d98cb55ebaf1917cea3e41939b1fb to your computer and use it in GitHub Desktop.
Fast File Finder. Lists all files under a specified folder regardless of character limitation on path depth. Based on Robocopy.
<#
Virtual multifunction via single function without additional parameters
Get-FolderItem - list files
Remove-FolderItem - delete files
Copy-FolderItem - copy files
Move-FolderItem - move files
#>
function Get-FolderItem {
[cmdletbinding(DefaultParameterSetName='Filter')]
[alias('Remove-FolderItem','Copy-FolderItem','Move-FolderItem')] #,'rfi','cfi','mfi','gfi'
param (
[parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[alias('FullName','InputObject')][string[]] $Path = $PWD,
[parameter(ParameterSetName='Filter')]
[string[]] $Filter = '*.*', # default robo filter
[parameter(ParameterSetName='Exclude')]
[alias('ef')][string[]] $ExcludeFile,
[parameter(ParameterSetName='Exclude')]
[alias('ed')][string[]] $ExcludeDir,
[switch] $Empty, # include empty folders; experimental
[switch] $PassThru, # option for Remove/Copy/Move-FolderItem; experimental
[alias('to')][string] $Destination = 'NULL', # option for Copy/Move-FolderItem; experimental
[switch] $Recursive,
[int] $Depth,
[alias('newer')] $MaxAge, # date | days
[alias('older')] $MinAge, # date | days
[alias('aolder')] $MinLad # date | days
)
begin {
if ($isMacos -or $islinux) {exit}
function convertto-days ($inputobject) {
$value = if ($inputobject -is [datetime]) {
(New-TimeSpan -Start $inputobject).TotalDays
}
elseif ($inputobject -as [int]) {[int]$inputobject}
else {
Write-Warning "[$inputobject] Invalid timespan argument specified."
exit
}
if ($value -lt 1900 -or "$value".Length -eq 8) {$value}
else {
if ($value -lt 0) {$value = -$value}
[datetime]::now.adddays(-$value).tostring('yyyyMMdd')
}
} # end convertto-days
if ($MyInvocation.InvocationName -match 'Copy-|^Move-') {
if (-not $Destination -or $Destination -eq 'NULL') {
Write-Warning 'Parameter set cannot be resolved using the specified named parameters.'
exit
}
$Destination = $Destination.trim([char[]]' "')
##if ($PSVersionTable.PSVersion.Major -eq 5 -and $Destination -match ' ') {$Destination = '"{0}"' -f $Destination}
} else {$Destination = 'NULL'}
$roboparam = [System.Collections.Generic.List[object]]::new()
$roboparam.AddRange(@("/NJH","/NJS","/BYTES","/FP","/NC","/NDL","/TS","/XJ","/MT:32","/R:0","/W:0"))
$list = ($MyInvocation.InvocationName -eq 'Get-FolderItem')
if ($list) {$roboparam.Add("/L"); $PassThru = $true}
if ($MyInvocation.InvocationName -eq 'Move-FolderItem') {$roboparam.Add("/PURGE")}
if (-not $PassThru) {$roboparam.Add("/NFL")} # skip any report
if ($Recursive) {$roboparam.Add("/S")}
elseif ($Empty) {$roboparam.Add("/E")}
if ($Depth -gt 0) {$roboparam.Add("/LEV:$Depth")}
if ($MinLad) {
$value = convertto-days $MinLad
$roboparam.Add("/MINLAD:$value")
}
if ($MaxAge) {
$value = convertto-days $MaxAge
$roboparam.Add("/MAXAGE:$value")
}
if ($MinAge) {
$value = convertto-days $MinAge
$roboparam.Add("/MINAGE:$value")
}
if ($ExcludeFile) {
$roboparam.Add("/XF $($ExcludeFile -join ',')")
}
if ($ExcludeDir) {
$roboparam.Add("/XD $($ExcludeDir -join ',')")
}
#$fileinfo = [regex]::new('^\s+(?<Size>\d+)\s(?<Date>\S+\s\S+)\s+(?<FullName>.+)',[System.Text.RegularExpressions.RegexOptions]::Compiled)
#$folderinfo = '^\s+(?<FileCount>\d+)\s+(?<FullName>.+)\s*'
} # begin
process {
if ($null -eq $roboparam -or $roboparam.count -lt 10) {return} # prevent unexpected behaviour
foreach ($item in $Path) {
$item = (Resolve-Path -Path $item -ErrorAction Stop).ProviderPath
if (-not (Test-Path -LiteralPath $item -Type Container -ErrorAction 4)) {
continue
}
if (-not $PassThru) {
$null = ROBOCOPY.EXE $item $Destination @filter @roboparam
continue
}
ROBOCOPY.EXE $item $Destination @filter @roboparam | . { process {
if ($_ -notmatch '^\s+(?<Size>\d+)\s(?<Date>\S+\s\S+)\s+(?<FullName>.+)\s*') {return}
$fn = $matches.FullName
[PScustomObject]@{
PStypename = 'System.IO.RobocopyFileInfo'
ParentPath = [io.path]::GetDirectoryName($fn)
FullName = $fn
Name = [io.path]::GetFileName($fn)
BaseName = [io.path]::GetFileNameWithoutExtension($fn)
Extension = [io.path]::GetExtension($fn)
Length = [int64]$matches.Size
LastWriteTime = [datetime]$matches.Date
FullPathLength = [int]$fn.Length
IsLongName = $fn.Length -gt 256
}
}}
}
} # process
} # END Get-FolderItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment