Powershell 正在尝试编写一个函数来复制目录的子文件夹和文件,但仅此不同 函数复制文件{ #.概要 #仅复制源和目标之间存在的文件差异 参数([object]$source,[object]$destination) #如果目标不存在,请创建目标。。。 mkdir$destination-Force-ErrorAction SilentlyContinue $source1=获取子项-路径$source $destination1=获取子项-路径$destination $filediff=比较对象-引用对象$source1-差异对象$destination1 $filediff | foreach{ $CopyParams=@{'Path'=$\\ InputObject.FullName} 如果($).SideIndicator-eq'

Powershell 正在尝试编写一个函数来复制目录的子文件夹和文件,但仅此不同 函数复制文件{ #.概要 #仅复制源和目标之间存在的文件差异 参数([object]$source,[object]$destination) #如果目标不存在,请创建目标。。。 mkdir$destination-Force-ErrorAction SilentlyContinue $source1=获取子项-路径$source $destination1=获取子项-路径$destination $filediff=比较对象-引用对象$source1-差异对象$destination1 $filediff | foreach{ $CopyParams=@{'Path'=$\\ InputObject.FullName} 如果($).SideIndicator-eq',powershell,Powershell,,这将复制两个文件夹中对象的差异,以便将第一个文件夹以外的所有文件复制到第二个文件夹,反之亦然。您可以将目标更改为用户的本地\AppData文件夹。希望这能为您提供一个良好的起点 function Copy-File { #.Synopsis # copies only the difference of files existing in between source and destination param([object]$source,[object]$destinati

,这将复制两个文件夹中对象的差异,以便将第一个文件夹以外的所有文件复制到第二个文件夹,反之亦然。您可以将目标更改为用户的本地\AppData文件夹。希望这能为您提供一个良好的起点

function Copy-File {
  #.Synopsis
  # copies only the difference of files existing in between source and destination 
  param([object]$source,[object]$destination)
  # create destination if it's not there ...
  mkdir $destination -Force -ErrorAction SilentlyContinue
  $source1 = Get-ChildItem -Path $source
  $destination1 = Get-ChildItem -Path $destination
  $filediff = Compare-Object -ReferenceObject $source1 -DifferenceObject $destination1
  $filediff | foreach { 
    $CopyParams = @{ 'Path' = $_.InputObject.FullName }
    if ($_.SideIndicator -eq '<=') {
      $CopyParams.Destination = $destination1
    } else {
      $CopyParams.Destination = $source1
    }
    Copy-Item @CopyParams
  }
}
函数获取文件夹($Message)
{
函数选择文件夹($Title=$Message,$path=0){
$object=新对象-comObject Shell.Application
$folder=$object.BrowseForFolder(0,$Title,0,$path)
如果($folder-ne$null){
$folder.self.Path
$Choice=$folder.Self.Path
}  
}  
选择文件夹-Message$Message-path$env:USERPROFILE
$selectedFolder=$this
}
功能启动文件夹选择
{
$script:firstFolder=获取文件夹-消息“选择引用文件夹”
$script:secondFolder=获取文件夹-消息“选择要比较的文件夹”
}
函数比较FolderContents
{
$firstFolderChildren=Get ChildItem$script:firstFolder
$secondFolderChildren=Get ChildItem$script:secondFolder
$script:folderComparison=比较对象-引用对象$firstFolderChildren-差异对象$secondFolderChildren
}
函数复制文件差异
{
$firstFolderDifference=$script:folderComparison |?{$\.SideIndicator-eq“=>”}
$secondFolderDifference=$script:folderComparison |?{$\.SideIndicator-eq“试试这个

Function Get-Folder($Message)
{

    Function Select-Folder($Title = $Message, $path = 0) {  
        $object = New-Object -comObject Shell.Application   

        $folder = $object.BrowseForFolder(0, $Title, 0, $path)  
        if ($folder -ne $null) {  
            $folder.self.Path  
            $Choice = $folder.Self.Path
        }  
    }  

    Select-Folder -Message $Message -path $env:USERPROFILE

    $selectedFolder = $this

}

Function Start-FolderSelection
{
    $script:firstFolder = Get-Folder -Message "Select the reference folder"
    $script:secondFolder = Get-Folder -Message "Select the folder to compare"
}

Function Compare-FolderContents
{
    $firstFolderChildren = Get-ChildItem $script:firstFolder
    $secondFolderChildren = Get-ChildItem $script:secondFolder

    $script:folderComparison = Compare-Object -ReferenceObject $firstFolderChildren -DifferenceObject $secondFolderChildren
}

Function Copy-FileDifferences
{
    $firstFolderDifference = $script:folderComparison | ? { $_.SideIndicator -eq "=>" }
    $secondFolderDifference = $script:folderComparison | ? { $_.SideIndicator -eq "<=" }

    foreach($difference in $firstFolderDifference)
    {
        $diffObj = $difference.InputObject
        Copy-Item $diffObj.FullName -Destination $script:firstFolder
    }
        foreach($difference in $secondFolderDifference)
    {
        $diffObj = $difference.InputObject
        Copy-Item $diffObj.FullName -Destination $script:secondFolder
    }
}

Function Run-Functions
{
    Start-FolderSelection
    Compare-FolderContents
    Copy-FileDifferences
}

Run-Functions

你正在寻找。
robocopy| |头痛
$destination="c:\tempcopy"
gci "c:\temp" | %{if (!(Test-Path "$destination\$($_.Name)")) {copy-item $_.FullName -Destination "$destination\$($_.Name)" -Force}    }