zoukankan      html  css  js  c++  java
  • 【转】PowerShell入门(二):PowerShell是Cmd命令行的加强版吗?

    转至:http://www.cnblogs.com/ceachy/archive/2013/01/31/PowerShell_vs_Cmd.html

     

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

    PowerShell中的命令

      启动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 
    Windows PowerShell 
    Copyright (C) 2012 Microsoft Corporation. All rights reserved.

    PS D:ProjectsPractisePowerShell> get-help

    TOPIC 
        Windows PowerShell Help System

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

    PS D:ProjectsPractisePowerShell> cmd 
    Microsoft Windows [Version 6.2.9200] 
    (c) 2012 Microsoft Corporation. All rights reserved.

    D:ProjectsPractisePowerShell>dir /ad 
    Volume in drive D is Doc 
    Volume Serial Number is A6C5-E7CE

    Directory of D:ProjectsPractisePowerShell

    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:ProjectsPractisePowerShell> 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替代。长远来看呢,谁知道呢。我只知道,多一份选择就多一份自由,多一种可能性Smile

  • 相关阅读:
    接口调用实现类&& 为什么Autowired定义在接口上
    getSuperclass与getGenericSuperclass区别
    Error resolving template “pages”, template might not exist or might not be accessible by any of the configured Template Resolver 或者 springboot使用thymeleaf时报html没有结束标签
    dcm4che-core导包失败! mvn pom文件导包总是失败
    使用IDEA springboot 如何通过mybatis-generator自动生成mapper dao model
    《剑指offer》第三十八题:字符串的排列
    《剑指offer》第三十七题:序列化二叉树
    《剑指offer》第三十六题:二叉搜索树与双向链表
    《剑指offer》第三十五题:复杂链表的复制
    《剑指offer》第三十四题:二叉树中和为某一值的路径
  • 原文地址:https://www.cnblogs.com/keepSmile/p/5802223.html
Copyright © 2011-2022 走看看