Windows Powershell command line equivalent of dd
Asked Answered
I

4

13

I am writing a Powershell script to make a raw copy of a drive and I have been unable to find a way to complete this.

On Linux, I would use 'dd' to perform this copy.

There are a handful of tools that can do this on Windows but none that I can control directly from the command line. (All have GUI interfaces)

Is there a method to make a physical copy of a drive through Powershell?

Thanks.

Insoluble answered 2/6, 2016 at 17:5 Comment(3)
There are various Win32 ports of dd (though not all have been maintained to work with the latest versions of Windows). There is no native, built-in PowerShell cmdlet for this, and while you could conceivably do it with hand-rolled raw disk access, that's almost certainly a bad idea (mostly because .NET, which PowerShell is based on, offers no easy way to do this). Windows does have files corresponding to raw disk volumes, just like Linux, but accessing them is much more convoluted.Labyrinthine
@JeroenMostert that might as well be the answer: Nothing out of the box but there are / may be 3rd party libraries to do this...Scowl
What are you're actually trying to accomplish? A dd style image isn't typically useful in a Windows environment. You could use diskpart to create a VHD, or diskpart and Mount-DiskImage (Win10) to repartition, format, and copy files to a USB from an .iso.Threemaster
M
6

I've been trying to do this for a while myself and I finally found a good answer.

Git for windows ships with the whole set of GNU core utilities (updated vs what you can find separately) including dd!

Just install Git for Windows or extract the portable version, from there inside of the install directory in git\usr\bin\ you will find the binaries for all of the GNU utils including dd (tested working)

Some further notes on usage in windows since \dev\sda\ isn't a thing:

$DiskDrives = Gwmi Win32_diskdrive | select DeviceID,BytesPerSector,Index,Caption,InterfaceType,Size,TotalSectors,SerialNumber | Out-GridView -OutputMode Multiple -Title 'Select Source Drive(s)'

$BaseOutputPath = 'D:\'
$DiskDrives | %{
. ('C:\Program Files\Git\usr\bin\dd.exe if={0} of={1} bs={2}' -f $_.DeviceID,(-join($BaseOutputPath,(- 
    join($Env:ComputerName,$_.Index)),'.img')),$_.BytesPerSector)
}

The included filename logic is just a placeholder, you can replace that parenthetical with a call to Read-Host if you want it to prompt you for the filename/path.

It is a bit annoying but you really do have to use WMI as the values returned by Get-Disk don't seem to work.

Monodrama answered 29/10, 2019 at 23:42 Comment(3)
Aren't you missing a -PassThru parameter on Out-GridView? Without that it should normally just display what's piped to it, but not return any objects.Gdynia
@Gdynia no, I used -OutputMode Multiple which allows you to select options in the resulting gui, then click ok which causes them to be output and then stored in $DiskDrivesMonodrama
Ah, indeed, that's functionally the same as -PassThru.Gdynia
B
2

You might already know that cygwin on Windows supports some Linux commands including dd. I have used it on several occasions to copy disks and load ISOs to USB and it works perfectly.

Bismuth answered 13/5, 2017 at 3:43 Comment(2)
TMO for me. Too Much Overhead. But I see your point. Meanwhile, I can't vote on this because ti does not answer the question as written (which came up in Google when I searched on almost exactly the title)Lineate
@Lineate What does "ti" mean and why doesn't this answer the question?Britanybritches
H
1
Get-CimInstance -ClassName Win32_DiskDrive | Format-List -Property DeviceID,BytesPerSector,Index,Caption,InterfaceType,Size,TotalSectors,SerialNumber

Following up @Chirishman answer, for Powershell 7.2, The Gwmi may missing from the powershell.

The alternative command to get the DeviceId and other info is available as above.

Then you can use dd if={DeviceId} of=<target_file>.

Haifa answered 29/9, 2022 at 4:24 Comment(0)
S
0

Windows 10 comes with linux now. Windows Subsystem for Linux. You can enable it as a feature. You can even get WSL 2 with the real kernel in 1903 & 1909: https://devblogs.microsoft.com/commandline/whats-new-in-the-windows-subsystem-for-linux-september-2020/

Shotputter answered 6/10, 2020 at 2:59 Comment(1)
Then, how to recognize the USB drive location? lsblk didn't workObelia

© 2022 - 2024 — McMap. All rights reserved.