Search code examples
powershellcmd

Are all CMD commands available within powershell?


Similar to other questions, but are ALL CMD commands usable within Powershell? In short, can I use a Powershell window to supplant CMD prompts, and do both CMD work and Powershell work in the same terminal?


Solution

  • Those commands that are built into cmd.exe (e.g., dir, type) are not directly callable in a PowerShell session, but you can call them via cmd /c ...; e.g.:

    # From PowerShell; calls the cmd.exe-internal `ver` command.
    cmd /c ver
    

    However, you'll find that most such commands have more powerful PowerShell counterparts.
    To ease the transition, some PowerShell commands (called cmdlets) have aliases named for their cmd.exe predecessors (e.g., dir is aliased to Get-ChildItem; use Get-Command <name> to find out what command a given name refers to).

    Note that PowerShell also provides superior replacements for external utilities; e.g., PowerShell's Select-String is a superior alternative to findstr.exe.
    Unlike the built-in cmd.exe commands, you can invoke such external utilities directly from PowerShell.

    You can run where.exe <name> to determine whether a given command name refers to a built-in cmd.exe command or an external utility: if it is the latter, its full path is printed.
    (This technique works from both cmd.exe and PowerShell, but in PowerShell you must include the .exe extension, because just where means something different: it is an alias of the Where-Object cmdlet.)

    In all these cases it's important to understand that PowerShell syntax works very differently and that the arguments you pass will be interpreted by PowerShell first:

    • Get-Help about_Parsing provides a high-level overview.

    • Notably, PowerShell has many more metacharacters than cmd.exe (more characters have special syntactical meaning).

    • Of particular interest when calling cmd /c or invoking an external utility is the PSv3+ stop-parsing token, --%, which treats the remainder of the command as if it had been invoked from cmd.exe; e.g.:
      cmd /c --% echo %windir%

      • Caveat: --% has many limitations and pitfalls - see the bottom section of this answer.