在powershell中使用get childitem时排除文件夹和文件

在powershell中使用get childitem时排除文件夹和文件,powershell,Powershell,我不确定如何排除以下内容: .env、web.config、节点模块 $sourceRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server" $destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli/server-sandbox" $dir = get-childitem $sourceRoot -Exclude .env, web.config, node_modules <-- How

我不确定如何排除以下内容:
.env、web.config、节点模块

$sourceRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server"
$destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli/server-sandbox"
$dir = get-childitem $sourceRoot -Exclude .env, web.config, node_modules <-- How do I exclude these       
$i=1
$dir| %{
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    $_ | copy -Destination $destinationRoot  -Recurse -Force
    $i++
}
$sourceRoot=“C:\Users\Wade Aston\Desktop\STEMuli\server”
$destinationRoot=“C:\Users\Wade Aston\Desktop\STEMuli/server sandbox”
$dir=get childitem$sourceRoot-Exclude.env、web.config、node_modules几个注意事项:
*尝试使用通配符来应用排除,如:*.env
*复制项参数源,允许使用类型字符串的集合。使用collection应该比使用foreach处理sequential更快!
*如果您只需要这些文件,可以考虑使用GET子项-文件< /P>
您可以尝试以下方法:

$Source = Get-ChildItem -Path C:\TEMP -Exclude dism.log, *.csv 
$dest = 'C:\temp2'

Copy-Item -Path $Source -Destination $dest -Force
希望有帮助

几个注意事项: *尝试使用通配符来应用排除,如:*.env *复制项参数源,允许使用类型字符串的集合。使用collection应该比使用foreach处理sequential更快! *如果您只需要这些文件,可以考虑使用GET子项-文件< /P> 您可以尝试以下方法:

$Source = Get-ChildItem -Path C:\TEMP -Exclude dism.log, *.csv 
$dest = 'C:\temp2'

Copy-Item -Path $Source -Destination $dest -Force

希望有帮助

要排除某些文件,如“*.env”和“web.config”,并排除具有特定名称的文件夹,可以执行以下操作:

$sourceRoot      = "C:\Users\Wade Aston\Desktop\STEMuli\server"
$destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server-sandbox"

$dir = Get-ChildItem -Path $sourceRoot -Recurse -File -Exclude '*.env', 'web.config' | 
       Where-Object{ $_.DirectoryName -notmatch 'node_modules' }

$i = 1
$dir | ForEach-Object {
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -Status $_  -PercentComplete $percent -Verbose

    $target = Join-Path -Path $destinationRoot -ChildPath $_.DirectoryName.Substring($sourceRoot.Length)
    # create the target destination folder if it does not already exist
    if (!(Test-Path -Path $target -PathType Container)) {
        New-Item -Path $target -ItemType Directory | Out-Null
    }
    $_ | Copy-Item -Destination $target -Force
    $i++
}

要排除某些文件,如“*.env”和“web.config”,并排除具有特定名称的文件夹,可以执行以下操作:

$sourceRoot      = "C:\Users\Wade Aston\Desktop\STEMuli\server"
$destinationRoot = "C:\Users\Wade Aston\Desktop\STEMuli\server-sandbox"

$dir = Get-ChildItem -Path $sourceRoot -Recurse -File -Exclude '*.env', 'web.config' | 
       Where-Object{ $_.DirectoryName -notmatch 'node_modules' }

$i = 1
$dir | ForEach-Object {
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -Status $_  -PercentComplete $percent -Verbose

    $target = Join-Path -Path $destinationRoot -ChildPath $_.DirectoryName.Substring($sourceRoot.Length)
    # create the target destination folder if it does not already exist
    if (!(Test-Path -Path $target -PathType Container)) {
        New-Item -Path $target -ItemType Directory | Out-Null
    }
    $_ | Copy-Item -Destination $target -Force
    $i++
}