PowerShell入门教程之PowerShell和Cmd命令行的关系?
PowerShell是命令行的加强版吗?PowerShell能执行命令行的所有命令吗?PowerShell要替代命令行?这三个问题的答案足以让我们了解PowerShell与Cmd命令行之间的关系。让我们慢慢道来。。。
PowerShell中的CMD命令
启动PowerShell,在其中输入几个常用的Cmd命令
PSD:\Projects\Practise\PowerShell>dir Directory:D:\Projects\Practise\PowerShell Mode LastWriteTime LengthName ---- ------------- ---------- d---- 1/23/2013 12:35PM d1 d---- 1/23/2013 12:35PM d2 -a--- 1/21/2013 8:38PM 36314alias.txt -a--- 1/21/2013 8:32PM 241530cmdlets.txt
或者
PSD:\Projects\Practise\PowerShell>cd.. PSD:\Projects\Practise>
结果跟我们的预期是差不多。不过,我们是否就能据此说PowerShell就是PowerShell是命令行的加强版吗?再试试下面这个命令:
PSD:\Projects\Practise\PowerShell>dir/ad dir:Cannotfindpath'D:\ad'becauseitdoesnotexist.
这个跟我们的期望相去甚远。在Cmd中,它应该输出当前位置的子文件夹的信息,但是这里,它似乎理解不了我们的参数。下面这个命令也是一样:
PSD:\Projects\Practise\PowerShell>fc.\alias.txt.\cmdlets.txt Format-Custom:Apositionalparametercannotbefoundthatacceptsargument'.\cmdlets.txt'.
我本来想要调用比较两个文件的fc命令,结果它却理解成了Format-Custom,牛头不对马嘴。这是怎么回事儿呢?到这里我们就可以回答前两个问题了:PowerShell不能执行Cmd命令行的所有命令,确切地说,PowerShell不能执行任何Cmd命令,至少不能直接执行。这是因为,PowerShell不是Cmd的新版本或者加强版什么的,只是它的有些命令从长相到功能都跟Cmd命令很相似而已。
PowerShell与Cmd命令行的关系
PowerShell可以做为一个应用程序在Cmd中运行,其运行方式有点像在Cmd中运行SQLCmd或者Nslookup,在没有明确exit退出之前,一直是应用程序的运行环境,一切的输入,包括命令和数据,都由应用程序来接受和处理。
D:\Projects\Practise\PowerShell>powershell WindowsPowerShell Copyright(C)2012MicrosoftCorporation.Allrightsreserved.
PSD:\Projects\Practise\PowerShell>get-help
TOPIC WindowsPowerShellHelpSystem