2020-11-26

Powershell Select Option Copy, But keep both files - by default

I've got this function, that copy files to an directory and rename the duplicates. It's works very well. But, for any repeated file that was found, it popups the window with the replace, or keep files and rename.

   Function Sync-FileArchive
{
[CmdletBinding(SupportsShouldProcess)]
[Alias('sfa')]

Param
(
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$SourceFiles, 

    [Parameter(Mandatory = $true, Position = 1)]
    [string]$Destination 
)

$Counter     = 0

Get-ChildItem -Path $SourceFiles | 
ForEach{
    Try
    {
        If(Test-Path -Path $Destination\$PSItem)
        {
            $Counter++
            Write-Warning -Message "$($PSItem.Name) already exits. Renaming destination file."
            Rename-Item -Path $Destination\$PSItem -NewName "$($PSItem.Basename)_$Counter$($PSitem.Extension)" -Force
        }
        Else
        {
            Write-Verbose -Message "$($PSItem.Name) does not exist. Copying file." -Verbose
            Copy-Item -Path $PSItem.Fullname -Destination $Destination     
        }
    }
    Catch {$PSItem.Exception.Message}
}}

Is there a way to select by default the "Copy, but keep both files" and don't show the window?

enter image description here



from Recent Questions - Stack Overflow https://ift.tt/3653VVn
https://ift.tt/2JaVlLV

No comments:

Post a Comment