小编Bar*_*tel的帖子

如何直接通过管道传输到 Copy-Item 而不是在 ForEach-Object 内

由于使用 -Recurse 标志时,Get-ChildItem 的 -Exclude 参数不会对子文件夹进行过滤,因此请参阅使用 get-childitem-exclude-parameter-in-powershell 中的其他无法排除目录的内容

但 -Exclude 参数可用于过滤掉根级别的文件夹

我写了自己的递归函数:

function Get-ChildItem-Recurse() {
    [cmdletbinding()]
    Param(
      [parameter(ValueFromPipelineByPropertyName = $true)]
      [alias('FullName')]
      [string[]] $Path,
      [string] $Filter,
      [string[]] $Exclude,
      [string[]] $Include,
      [switch] $Recurse = $true,
      [switch] $File = $false
    )

    Process {
      ForEach ( $P in $Path ) {
        Get-ChildItem -Path $P -Filter $Filter -Include $Include -Exclude $Exclude | ForEach-Object {
        if ( -not ( $File -and $_.PSIsContainer ) ) {
          $_
        }
        if ( $Recurse -and $_.PSIsContainer ) {
          $_ …
Run Code Online (Sandbox Code Playgroud)

powershell pipeline copy-item foreach-object

2
推荐指数
1
解决办法
915
查看次数

标签 统计

copy-item ×1

foreach-object ×1

pipeline ×1

powershell ×1