Copy contents of network drive to local folder I have an automated third party backup places files on a network drive.

I've mapped this drive to a local folder on the DB server using the following PowerShell script

$pass ="xxxx"|ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PsCredential('MERIVALEHOSTED\SVC.APPSQL',$pass)

New-PSDrive -Name K -PSProvider "FileSystem" -Credential $cred -Root '\\10.0.30.113\sqlbackup\ACCCluster01$AccWarehouseAG';
Get-PsDrive;


This maps my network drive to the K drive.

I then want to copy the entire folder structure of this (it's the output of Ola Hallengrens backup - DIFF/FULL/LOGS) to a local drive, but not before deleting the current contents of this folder. 

This is the first step of a automated DB restore. I've been able to automate the restoration process successfully using the DbaTools powershell module, but the file copy isn't working.

I've tried both:

Copy-Item -path K:\*.* -destination M:\VoxtronRestore\;


as well as this:

function Copy-Directories 
{
    param (
        [parameter(Mandatory = $true)] [string] $source,
        [parameter(Mandatory = $true)] [string] $destination        
    )

    try
    {
        Get-ChildItem -Path $source -Recurse -Force |
            Where-Object { $_.psIsContainer } |
            ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
            ForEach-Object { $null = New-Item -ItemType Container -Path $_ }

        Get-ChildItem -Path $source -Recurse -Force |
            Where-Object { -not $_.psIsContainer } |
            Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
    }

    catch
    {
        Write-Host "$_"
    }
}

$source = "K:\"
$dest = "M:\VoxtronRestore"

Copy-Directories $source $dest


The first of which fails to copy anything, and the second gives me an error about overwriting the source with itself.

I am wondering if anyone has any better suggestions or modifications to the above which will enable this to complete successfully.
*.* will only return the files directly under K:\ (You probably have a folder with the database/instance name here). Use Copy-Item K:\ -Recurse M:\VoxtronRestore instead.