Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Created December 20, 2017 17:12
Show Gist options
  • Save turboBasic/1cb5ced09bfbf8f36deac04109c8514b to your computer and use it in GitHub Desktop.
Save turboBasic/1cb5ced09bfbf8f36deac04109c8514b to your computer and use it in GitHub Desktop.
Find-Path.ps1 returns paths of all existing filesystem items provided in form of glob, PathInfo or FileInfo objects. #powershell
function Find-Path {
<#
.EXAMPLE
Find-Path -path @('hgjf', '.', $null)
.EXAMPLE
Get-ChildItem . -recurse -filter *.ps1 | Find-Path
.EXAMPLE
'**/*.txt', (Get-ChildItem . -recurse) | Find-Path -ExcludeDirectory
#>
[CmdletBinding()]
[OutputType( [String], [String[]] )]
Param(
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Alias( 'FullName' )]
[Object[]]
$Path,
[Parameter( Mandatory = $False )]
[Alias( 'NoDirectory' )]
[Switch]
$ExcludeDirectory,
[Parameter( Mandatory = $False )]
[Alias( 'NoFile' )]
[Switch]
$ExcludeFile
)
BEGIN {}
PROCESS
{
foreach ($pathItem in $Path)
{
$items = @()
if ($pathItem -is [System.String]) {
$items += $(
try {
(Resolve-Path $pathItem -ErrorAction SilentlyContinue).Path
}
catch { }
)
} elseif ($pathItem -is [System.IO.FileSystemInfo]) {
$items += $pathItem.FullName
} elseif ($pathItem -is [System.Management.Automation.PathInfo]) {
$items += $pathItem.Path
} elseif ($pathItem -is [System.Array]) {
$items += $pathItem |
ForEach-Object {
Find-FilePath -Path $_
}
}
$items |
Where-Object {
try { Get-Item -Path $_ -ErrorAction SilentlyContinue }
catch { $False }
} |
Where-Object {
$isContainer = (Get-Item -Path $_).psIsContainer
-not ( $ExcludeDirectory -and $isContainer ) -and
-not ( $ExcludeFile -and -not $isContainer)
}
}
}
END {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment