当前位置: 移动技术网 > IT编程>脚本编程>Shell > PowerShell入门教程之PowerShell和Cmd命令行的关系?

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

2017年12月08日  | 移动技术网IT编程  | 我要评论

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

powershell中的cmd命令

启动powershell,在其中输入几个常用的cmd命令

复制代码 代码如下:

ps d:\projects\practise\powershell> dir
    directory: d:\projects\practise\powershell
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:\projects\practise\powershell> cd ..
ps d:\projects\practise>

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

ps d:\projects\practise\powershell> dir /ad
dir : cannot find path 'd:\ad' because it does not exist.

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

复制代码 代码如下:

ps d:\projects\practise\powershell> 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:\projects\practise\powershell>powershell
windows powershell
copyright (c) 2012 microsoft corporation. all rights reserved.

ps d:\projects\practise\powershell> get-help

topic
    windows powershell help system

cmd也可以在powershell中作为应用程序运行,其运行方式与在cmd中运行powershell相似:

复制代码 代码如下:

ps d:\projects\practise\powershell> cmd
microsoft windows [version 6.2.9200]
(c) 2012 microsoft corporation. all rights reserved.

d:\projects\practise\powershell>dir /ad
volume in drive d is doc
volume serial number is a6c5-e7ce

directory of d:\projects\practise\powershell

01/30/2013  04:54 pm    <dir>          .
01/30/2013  04:54 pm    <dir>          ..
01/23/2013  12:35 pm    <dir>          d1
01/23/2013  12:35 pm    <dir>          d2

powershell通过alias这个特性,可以让使用者以cmd风格来使用powershell命令。这样做的好处是让用户在刚接触powershell的时候,就像在使用cmd一样亲切、熟悉。坏处是,让人容易把powershell和cmd搞混了。不过当你了解了alias的概念和get-alias命令以后,这个问题将迎刃而解:

复制代码 代码如下:

ps d:\projects\practise\powershell> get-alias dir, echo, type

commandtype     name                                               modulename
-----------     ----                                               ----------
alias           dir -> get-childitem

alias           cd -> set-location
alias           echo -> write-output

就是说dir实际上是powershell的get-childitem命令的一个别名,cd是set-location的别名。。。到这里powershell和cmd的关系问题算是解决了。

cmd命令行会被powershell替代吗?

  我其实一直以来都很讨厌类似“谁不如谁好”、“谁要代替谁”的问题,放到这里也一样,新的东西必然有它的优越性,旧的东西也有一票粉丝。从现在powershell被接受的程度来看,短时间内cmd不会被powershell替代。长远来看呢,谁知道呢。我只知道,多一份选择就多一份自由,多一种可能性。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网