简体   繁体   中英

Get-ChildItem recursively but exclude files in the parent folder

I need to get a list of files in all subdirectories of $PSScriptRoot but exclude any files in the parent folder of $PSScriptRoot.

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer}

You could list the parent folders first, then recurse each directory for files:

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Directory | Get-ChildItem -File -Recurse

You can add a Where-Object clause to filter out all files that are directly inside the $PSScriptRoot:

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse -File | Where-Object { $_.DirectoryName -ne $PSScriptRoot}

you can use switch -File instead of Where-Object {.$_.PSIsContainer} as of PowerShell version 3.0

You just need to call Get-ChildItem again as a pipe

$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer} | Get-ChildItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM