Executing DOS command with Powershell

Published on: 16th Dec 2019

Updated on: 16th Jan 2022

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

Back to #POWERSHELL blog

Back to #blog listing

Author

Lau Hon Wan, software developer.