简体   繁体   中英

Windows Powershell command line equivalent of dd

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.

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.

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.

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/

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM