I want to be able to copy items (files, folders) from a Windows PC to an MTP device. I want to do it with PowerShell for scripting purposes.
I found this thread: shell32 copyhere not working neither in .Net nor powershell script but the answer there does not help understanding the solution (and was given by the same person who asked that question). Below there is a minimal code example:
param ($itemName)
$shell = New-Object -com Shell.Application
$sourceFolder = $shell.Namespace($(Get-Location).toString()).self
$item = $sourceFolder.GetFolder.Items() | where { $_.Name -eq $itemName }
$mtpDevice = $shell.NameSpace(0x11).items() | where { $_.name -eq 'DUMMY_MODEL' }
$destination = $mtpDevice.GetFolder.items() | where { $_.Name -eq 'Card' }
$destinationFolder=$shell.Namespace($destination).self
$destinationFolder.GetFolder.CopyHere($item)
$shell.open($destinationFolder)
Start-Sleep -s 1
I assume that the item to be copied ($itemName) exists on the Windows machine. I assume that the mtp device is seen as "DUMMY_MODEL" in Windows Explorer and that it contains an empty, top-level folder "Card".
I expect that the line
$destinationFolder.GetFolder.CopyHere($item)
should do the job. But it is not the case. In order for it to work I need to programmatically open the destination folder window and use sleep. Why? The above mentioned thread says that it is in order for the copying thread to finish. Why can't it finish without opening the window? Can this be done without programmatically opening the window? And even if I do open the window and do the sleep, copying does not work 100% reliably. Why?