Run DOS command or CMD in Powershell


Explanation

It's best that we can write all scripts in Powershell.... that will be very nice. Unfortunately, sometimes it's more convenient to write the DOS command.

To execute DOS command, we use "cmd /c" + the DOS command:

cmd /c "dir d:"

We may execute the DOS command using Invoke-Expression:

$cmd = "dir d:\temp3"
Invoke-Expression -Command $cmd

OR, to execute the Robocopy to copy the file from one folder to another:

$src = "d:\temp3\test"
$dst = "d:\temp3\test2"
$cmd = "robocopy $src $dst /S /E /COPYALL /DCOPY:T /R:10 /W:10 /NP /TEE"
Invoke-Expression -Command $cmd

Related posts

Back to #POWERSHELL blog

Back to #blog listing