Windows Powershell command line equivalent of dd - windows

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

Related

Universal copy command for Windows and Linux

I was just curious that is there any universal command to copy files. Which should work for both Windows and Linux system.
I know Windows uses copy command and Linux uses cp command. But is there any universal command to do so.
If powershell is installed in your Windows OS,
then you can use the built-in command cp as well, just like Linux does.
UNIX/Linux, Mac, and Windows can all run PowerShell Core 6. https://github.com/powershell/powershell
The command is Copy-Item, but there are three (3) aliases. Using an alias at a command prompt is fine, but they should not be put into script files.
>Get-Alias -Definition Copy-Item
CommandType Name
----------- ----
Alias copy -> Copy-Item
Alias cp -> Copy-Item
Alias cpi -> Copy-Item
Since there is no "universal" command, something will need to be installed on every machine except those that already have the command you choose to make "universal." At a minimum, it would be a .bat/.cmd script on Windows or a shell script on UNIX/Linux/Mac. Choosing to implement it in Python or any other language would require that language to be installed.
There is no universal command to do this, but you have several workarounds:
use cygwin or msys on Windows to have the Linux equivalent commands on windows
use Windows Powershell (partially compatible, but works for cp)
provided that your environments all have python installed, write a python script that does what you want .

Convert localized path to the English

if user installs windows in his native language(not english) he can access files using localized paths ie. insted C:\Users\UserName\Desktop he can access Desktop using C:\LocalizedUsersName\LocalizedDesktopName Is there any Powershell command which can convert/translate local version of the Path to the English version?
There is nothing specifically for a path, yet, basically, along the same lines as you'd do this for other strings you are working with. See the discussions, examples, and answers below.
International Settings Cmdlets in Windows PowerShell
Using the Microsoft Translator API from PowerShell
Localization and PowerShell
plattsoft_PSUICultureExample
Forcing PowerShell errors output in English on localized systems
You can change the pipeline thread's CurrrentUICulture like so:
[Threading.Thread]::CurrentThread.CurrentUICulture = 'fr-FR'; Get-Help
Get-Process
I'm on an English system but before I executed the line above, I
updated help like so: Update-Help -UICulture fr-FR
$ENUS="[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'"
then invoke-expression $ENUS; Get-Help Get-Process "invoke-expression
$ENUS;" is little shorter if you need to run many commands

Double driver export script

I'm terrible at scripting, so I would like some help with a script which does the following:
a wmic query:
wmic computersystem get model
Latitude E7450
creates a folder in the root using the query output without spaces:
Latitude_E7450
Then run double driver to backup all the drivers and storing them in the newly created folder:
ddc b /source:"c:\Windows" /target:"c:\Latitude_E7450"
You can get name for the target folder from the WMI Model using powershell, assuming WMI returns this (some OEM units don't),
$dirName = (Get-WmiObject -Class win32_computersystem).Model
Then you can call Double Driver,
& ddc.exe b /source:"C:\Windows" /target:"C:\$dirName"
For a pure powershell solution, see for example this post from Mikael Nyström, "PowerShell is King – Export drivers from Windows"
Export-WindowsDriver -Destination "C:\Drivers\$((Get-WmiObject -Class win32_computersystem).Model)" -Online
This requires Windows 6.3 at least (8.1 or 2012 R2), so use Double Driver for Win 7, if you can still find it. For even more bells and whistles, see "Building Configuration Manager Driver Packages for Windows 7 with PowerShell and Double Driver".

Scan for new hardware using a bat file?

I have a batch installer that overrides my usb devices drivers.
how can I force my windows to scan for hardware changes using a batch file?
START /WAIT RunDll32.exe Syssetup.dll,UpdatePnpDeviceDrivers
1 The best way I found is:
powershell -windowstyle hidden -command "& {\"rescan\" | diskpart}"
For the detail, you can refer to this link :"Use the rescan command to rescan all I/O buses and cause any new disks that have been added to the computer to be discovered."
For Windows 2008/7 and above, the powershell and diskpart is shipped with OS.
Or just run it without powershell:
echo rescan | diskpart
2 The other way is use the MS command line tool, Devcon, which you have to download it first
You can use it to do a lot of things, including enable/disable/rescan all kind of device(not only disk), update device driver, ... even on the remote machine.
You also can see the source code of it in this link
Windows 10 ships with PnPUtil.exe. Run pnputil.exe /scan-devices from administrative command prompt.
https://serverfault.com/a/1060172/365042

How can I view a log file from powershell console ? (i.e. powershell equivalent of 'less')

What is the powershell equivalent of 'less'?
I see 'more', but it lacks some of the features I rely on (e.g. searching through the file)
I seek a pager (equivalent of 'less') which allows searching (match or ignore case), multiple files at once, etc.
Some of our servers run windows 2008 and I lack admin privileges to install cygwin
I had heard windows 2008, MSFT got their act together and provided some easy-for-admins tools.
Update:
I should give some context:
I know little about power shell
New servers have 2008 on them
While I affection for many tools of yore, the dos prompt is not one of them
I was hoping that Powershell had the equivalent of grep,ls,less, xargs, et
I understood that powershell gave us those tools
I fired off my question quickly.
thanks
It reads like you know you can do this:
gc logfile.log | more
(GC is an alias for Get-Content).
You may be able to do the filtering etc.. with this more information can be found by running these commands:
Get-Help Get-Content Get-Help
Get-Content -Examples
(Get-Help gc would work fine as well).
And the bits you may be interested in are limit\filter etc...
Get-Help gc -Parameter * | more
I just use the GOW version of less, works fine.
I don't know of any direct analogue for less in powershell that you can implement easily. Your best bet is to get a windows implementation of less that is outside of cygwin, that way you can just drop in the binary somewhere accessible to your account.
to get grep/vim/wget and other Linux like commands in powershell I suggest running.
iex (new-object net.webclient).downloadstring(‘https://get.scoop.sh’)
then
scoop install grep
scoop install perl
scoop install vim
and to get a list of all of them
scoop search
In Windows 10 PowerShell + Cygwin I use:
gc .\myfile.log | less
Previously I was trying to use cygwin directly:
less .\myfile.log
but it shows binary file because of invalid charset setting between 32b-bit and 64-bit.
I was hoping that Powershell had the equivalent of grep,ls,less, xargs, et
In the case you missed this question (top voted) you might enjoy this answer.

Resources