PowerShell入门教程之PowerShell和Cmd命令行的关系?

2019-09-30 13:40:14王振洲

PowerShell是命令行的加强版吗?PowerShell能执行命令行的所有命令吗?PowerShell要替代命令行?这三个问题的答案足以让我们了解PowerShell与Cmd命令行之间的关系。让我们慢慢道来。。。

PowerShell中的CMD命令

启动PowerShell,在其中输入几个常用的Cmd命令

PS D:ProjectsPractisePowerShell> dir
    Directory: D:ProjectsPractisePowerShell
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         1/23/2013  12:35 PM            d1
d----         1/23/2013  12:35 PM            d2
-a---         1/21/2013   8:38 PM      36314 alias.txt
-a---         1/21/2013   8:32 PM     241530 cmdlets.txt

或者

PS D:ProjectsPractisePowerShell> cd ..
PS D:ProjectsPractise>

结果跟我们的预期是差不多。不过,我们是否就能据此说PowerShell就是PowerShell是命令行的加强版吗?再试试下面这个命令:

PS D:ProjectsPractisePowerShell> dir /ad
dir : Cannot find path 'D:ad' because it does not exist.

这个跟我们的期望相去甚远。在Cmd中,它应该输出当前位置的子文件夹的信息,但是这里,它似乎理解不了我们的参数。下面这个命令也是一样:

PS D:ProjectsPractisePowerShell> fc .alias.txt .cmdlets.txt
Format-Custom : A positional parameter cannot be found that accepts argument '.cmdlets.txt'.

我本来想要调用比较两个文件的fc命令,结果它却理解成了Format-Custom,牛头不对马嘴。这是怎么回事儿呢?到这里我们就可以回答前两个问题了:PowerShell不能执行Cmd命令行的所有命令,确切地说,PowerShell不能执行任何Cmd命令,至少不能直接执行。这是因为,PowerShell不是Cmd的新版本或者加强版什么的,只是它的有些命令从长相到功能都跟Cmd命令很相似而已。

PowerShell与Cmd命令行的关系

  PowerShell可以做为一个应用程序在Cmd中运行,其运行方式有点像在Cmd中运行SQLCmd或者Nslookup,在没有明确exit退出之前,一直是应用程序的运行环境,一切的输入,包括命令和数据,都由应用程序来接受和处理。

D:ProjectsPractisePowerShell>powershell