I'm trying to access MTP device storage to automate file copy, backup, etc. If Windows Explorer is able to open and browse Android device Internal Storage and connected SD card, how can I access these storages with PowerShell?
I've found a lots of hints, such as "get device ID and use WMI"
What is the way the Windows Explorer uses to open and browse especialy Android's storages?
Is it possible to use some System.IO Class like here?
$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
return @($r)[-1]
}
I'm able to access and browse content of local MTP (android) device with this:
$WIAdialog = New-Object -ComObject “WIA.CommonDialog”
$Device = $WIAdialog.ShowSelectDevice()
$Device = $WIAdialog.ShowAcquireImage()
$Device = $WIAdialog.ShowAcquisitionWizard()
$Device = $WIAdialog.ShowDeviceProperties()
$Device = $WIAdialog.ShowItemProperties()
$Device = $WIAdialog.ShowPhotoPrintingWizard()
$Device = $WIAdialog.ShowSelectItems()
$Device = $WIAdialog.ShowTransfer()
But this code is loading information about stored pictures which is too slow even at local computer. Is it possible to avoid loading info about pictures to speed up loading and accessing remotely connected devices?
Thanks in advance for any information and hints!
This is my first answer on Stack Overflow (Stack Exchange) please take it into the consideration.
I am dealing with the same issue - I want to be able to work with android internal storage in PowerShell as I am able in Explorer.
My only suggestion is that we could install FTP Server (my favourite is FTP Server)
and sort it this way. What I have found so far are these answers on superuser:
Open command prompt to access folders of a USB connected Android phone
13 votes & accepted
In order to assign a drive letter to a removable device, that device must support UMS (USB Mass Storage) protocol. Unfortunately most newer Android phones, especially those without a removable SD Card, do not support UMS. Instead, they support MTP (Media Transfer Protocol) and PTP (Picture Transfer Protocol) protocols. In such devices it's not possible to map storage as a drive in Windows.
See more details on this Superuser.com question: How do I access MTP devices on the command line in Windows?
24 votes & accepted
Unfortunately, APIs exposed by MTP are very different from a normal filesystem APIs. Therefore exposing MTP device as a read/write filesystem is not possible. The main reason:
Wikipedia says:
Neither the MTP nor the PTP standards allow for direct modification of objects. Instead, modified objects must be reuploaded in their entirety, which can take a long time for large objects. With PTP/MTP, the file size must be known at the opening stage.
Your common file copy program just opens a source and a target file, and copies data in chunks from the source file to the target. This won't work with MTP, since you need to use MTP special functions, and generic filesystem primitives (read, seek, write) are not available.
There are also other limitations. For example, the number of files that can be read or written simultaneously on an MTP device is severely limited. The device simply does not behave like a filesystem.
I suppose read-only filesystem driver for an MTP device might be possible, but because of the problems outlined above, it will be of very little use, so nobody bothered to create it.
edited May 12 '12 at 6:33 Peter Mortensen 8,075
answered Jan 10 '12 at 20:08 haimg 15.9k
The read-only filesystem driver seems to exist now: ptpdrive.com – Arne de Bruijn Sep 12 '13 at 12:25
ptpdrive.com
Actually, it's not "not possible". When you consider that I've got gphotofs and mtpfs as FUSE filesystems on Linux that're COMPLETELY Read/Write - its' quite possible to accomplish this as a "drive letter" under Windows... they've just not made it available or easy. – Svartalf May 9 '14 at 19:57
With that said, on some selected Samsung and Sony Android devices it's possible to enable the UMS mode for external storage only (SD Card). See this app SG USB Mass Storage Enabler.
Also, if your goal to simply copy files to and from an Android device via command prompt, ADB will allow you to do so. This utility is part of Android SDK tools. You will need USB drivers for your android phone to be installed, USB Debugging activated in Developer Settings on the phone, and authorize the PC to debug the phone (via a prompt on the device.) After that is done, you will be able to use adb push
and adb pull
commands to copy files & directories, and various Linux shell commands via adb shell <command>
(e.g.adb shell ls /sdcard/
) to navigate the directory structure on the phone.
edited Mar 20 '17 at 10:18
Community♦
1
answered Nov 21 '13 at 16:14
Chahk
18.5k
App mentioned in above answer looks to me very limiting because its age.
Function fn_PhoneFolder {
Param($PhoneName, $PhoneFolder)
Write-Host "Searching....$($PhoneName)" -ForegroundColor Green
$Shell = new-object -com Shell.Application
$ShellItem = $Shell.NameSpace(17).self #17 = My Computer (https://learn.microsoft.com/en-gb/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants?redirectedfrom=MSDN)
$Phone = $ShellItem.GetFolder.items() | where { $_.name -eq $PhoneName }
$FolderParts = @( $PhoneFolder.Split([system.io.path]::DirectorySeparatorChar) )
$PhonePath = $Phone
ForEach ($Part in $FolderParts) {
If ($Part) {
$PhonePath = $PhonePath.GetFolder.items() | where { $_.Name -eq $Part }
}
}
$ReturnFiles = New-Object System.Collections.Generic.List[System.Object]
$Files = @($PhonePath.GetFolder.items())
$Files
ForEach ($File in $Files) {
$TempFiles = @{}
$TempFiles.Name = $File.Name
$TempFiles.Path = $File.Path
$TempFilesObject = New-Object PSobject -Property $TempFiles
$ReturnFiles.Add($TempFilesObject)
}
Return $ReturnFiles
}
$MyPhoneFiles = fn_PhoneFolder -PhoneName "X19" -PhoneFolder "Internal shared storage\WhatsApp\Media\WhatsApp Images\Sent"
Change PhoneName and PhoneFolder on the last line to match your Phone Name as displayed in My Computer. This function will pull back an array of all files in the folder selected. Thanks to https://blog.daiyanyingyu.uk/2018/03/20/powershell-mtp/ for the idea of searching through the shell folders to find the phone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With