Using a command line tool within PowerShell script

Publish Date: June 2, 2022

Using a command line tool within PowerShell script

PowerShell has now become most popular than every before. People who switches from the traditional command prompt (cmd.exe) to PowerShell can conveniently use the commands that used to work in command prompt right inside the PowerShell console too. But when it comes to PowerShell scripting they always run into issues because PowerShell scripts often mis-interpret the regular command line tools and scripts break. Although PowerShell offers every cmdlet and alias to perform whatever you used to do in cmd.exe but there are still some situations when you can’t help but have to use command-line tool in PowerShell script.

In this article, I will tell you how to use regular command-line tools within a PowerShell script with the help of useful examples. So lets get started.

Using the “cmd /c” Switch

To use a command line tool within PowerShell script, first thing you could do it use the cmd /c inside the PowerShell script. For example, you are trying to reset the NTFS permissions of a directory but you’re still learning PowerShell. Being an experienced admin, you already know how to set-up permissions using iCacls. Even though PowerShell offers Get-Acl and Set-Acl cmdlets for managing permissions but since you’re already learning PowerShell, you are motivated to use PowerShell script. In such case you will of-course think of using icacls command in PowerShell script – which is not bad at all. Take a look at the following code:

$dirs = Get-ChildItem -Path "C:\inetpub" -Include "App_Data" -Directory -Recurse -Force -ea 0
foreach($dir in $dirs) {
    cmd /c "icacls $dir /inheritance:d /c"
    cmd /c "icacls $dir /grant IIS_IUSRS:(OI)(CI)F /t /c"
}

Let me briefly explain what this script is doing. It recursively finds a directory named App_Data inside C:\inetpub parent directory and then use two icacls commands. First command disables the inheritance and the second command grants a full control permission on that directory to IIS_IUSRS built-in identity. Note how cmd /c is used and the actual icacls command and its arguments are enclosed in quotes.

Using the Call Operator (&)

If you do not like the above method, you could use the & character which is known as call operator or invocation operator in PowerShell. Suppose you convert .mp4 videos into .mkv format using ffmpeg.exe which is a command-line tool. You want to create a PowerShell script to automate the video conversion process. To do that you could use the & character as shown in the following command:

& "D:\tools\ffmpeg.exe"

The use of "quotes" is forcing PowerShell to treat the quoted text as a regular string and the use of & character is causing the quoted string to be executed as a command. As a result your ffmpeg.exe tool is executed. Another real world example of using the call operator is running PsExec in PowerShell script.

Background Operator vs Call Operator

Please don’t confuse the call operator with the background operator. Both of them are referred by the same character which is ampersand (&) but here is the difference:

  • If you put & before any command or script, PowerShell treats the text followed by & character as a regular string or a script. Hence known as call operator.
    Example:

    & "D:\MyScripts\awesome_script.bat"
  • If you put & after any command or script, it takes the command to the background (as in Start-Job cmdlet). Hence known as background operator.
    Example:

    Do-Something &


Microsoft Certified | Cisco Certified

Leave a Reply

back to top