PowerShell---学习手记(1)

本文为个人学习资料整理,内容整理自《Windows Powershell in Action》、网络和本人瞎编。

什么是PowerShell

PowerShell is the new command-line/scripting environment from Microsoft. The overall goal for this project was to provide the best shell scripting environment possible for Microsoft Windows. This statement has two parts, and they’re equally important, because the goal wasn’t just to produce a good generic shell environment but rather to produce one designed specifically for the Windows environment. Although drawing heavily from existing command-line shell and scripting languages, the PowerShell language and runtime were designed from scratch to be an optimal environment for the modern Windows operating system.

为什么需要PowerShell

这段来自互联网。

 曾几何时,微软的服务器操作系统因为缺乏一个强大的Shell备受诟病。而与之相对,Linux的Shell可谓丰富并且强大。Windows Server的Shell,也就是从Dos继承过来的命令行,处理简单问题尚可,一旦遇到稍微复杂一点的问题,它就会把本已复杂的问题,弄得更加复杂。 引入VBScript,使得Windows Server管理员处理问题等效率提高了不少。但VBScript是个脚本语言,即缺乏Shell的简单性,也不能利用高级语言的诸多优点。这使得微软迫切需要一个强大的Shell来管理服务器,甚至整个网络。在这种前提下,PowerShell诞生了。

  • 我觉得学习PowerShell的意义在于:

  • 集成于Windows,无需配置环境。

  • 基于.Net Fromework,对(.Net)程序员友好。
  • Windows服务器没别的脚本好用。
    • PowerShell 与 Cmd

这里有个误区,很多人觉得cmd和powershell是一个东西,或者powershell是cmd的升级版。

具体差异我们往下看。
先做个对比:

  • Cmd命令
C:\Users\李鹏翔>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 40D7-4572
 C:\Users\李鹏翔 的目录
2016/11/01  15:54    <DIR>          .
2016/11/01  15:54    <DIR>          ..
2016/11/01  16:45    <DIR>          .atom
2016/11/01  15:49             1,737 .bash_history
2016/10/27  18:38                56 .gitconfig
2016/10/25  18:52    <DIR>          .idlerc
  • PowerShell命令
PS C:\Users\李鹏翔> dir
    目录: C:\Users\李鹏翔
Mode                LastWriteTime         Length Name
d-----        2016/11/1     16:45                .atom
d-----       2016/10/25     18:52                .idlerc
d-----       2016/10/24     20:55                .oracle_jre_usage
d-----       2016/10/24     20:58                .PyCharm2016.2
d-----       2016/10/31     18:38                .ssh
d-----       2016/10/28     13:43                .VirtualBox
d-r---       2016/10/29     22:17                Contacts
可以看到同样的命令输出的结果是相同的,这是不是说PowerShell就是Cmd的拓展呢?
  • 关系

PowerShell不能执行Cmd命令行的所有命令,确切地说,PowerShell不能执行任何Cmd命令,至少不能直接执行。这是因为,PowerShell不是Cmd的新版本或者加强版什么的,只是它的有些命令从长相到功能都跟Cmd命令很相似而已。

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

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

给你个栗子:

PS C:\Users\李鹏翔> cmd
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。
C:\Users\李鹏翔>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 40D7-4572
 C:\Users\李鹏翔 的目录
2016/11/01  15:54    <DIR>          .
2016/11/01  15:54    <DIR>          ..
2016/11/01  16:45    <DIR>          .atom
2016/11/01  15:49             1,737 .bash_history
2016/10/27  18:38                56 .gitconfig
2016/10/25  18:52    <DIR>          .idlerc
2016/10/24  20:55    <DIR>          .oracle_jre_usage
2016/10/24  20:58    <DIR>          .PyCharm2016.2
2016/10/31  18:38    <DIR>          .ssh

Alias

是的,别名。
PowerShell就是通过这个来让自己和Cmd的命令相似甚至相同。
在PowerShell当中通过Alias命令可以看到所有设置的别名。
知道了这个之后也更容易讲PowerShell与Cmd区分开来。

PS C:\Users\李鹏翔> alias
CommandType     Name                                               Version    Source
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
Alias           cat -> Get-Content
Alias           cd -> Set-Location
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility
Alias           chdir -> Set-Location
Alias           clc -> Clear-Content
Alias           clear -> Clear-Host
Alias           clhy -> Clear-History
Alias           cli -> Clear-Item
Alias           clp -> Clear-ItemProperty
Alias           cls -> Clear-Host
Alias           clv -> Clear-Variable
Alias           cnsn -> Connect-PSSession
Alias           compare -> Compare-Object
Alias           copy -> Copy-Item
Alias           cp -> Copy-Item
Alias           cpi -> Copy-Item
Alias           cpp -> Copy-ItemProperty

知识拓展

Scripting languages vs. shells

If this is the case, then what is scripting and why are scripting languages not shells?
To some extent, there’s no difference.(……)

Many scripting languages have a mode in which they take commands from the user and then execute those commands to return results. This mode of operation is called a Read-Evaluate-Print loop, or REPL. Not all scripting languages have these interactive loops, but many do.

In what way is a scripting language with a Read-Evaluate-Print loop not a shell?
The difference is mainly in the user experience.
A proper command-line shell is also a proper user interface.As such, a command line has to provide a number of features to make the user’s experience pleasant and customizable.
The features that improve the user’s experience include aliases (shortcuts for hard-to-type commands), wildcard matching so you don’t have to type out full names, and the ability to start other programs without having to do anything special such as calling a function to start the program.

Command-line shells provide mechanisms for examining, editing, and re-executing previously typed commands.
These mechanisms are called command history.

总结

对于使用过Xnix下命令行的同学来说,学习PowerShell只是一个重新熟悉语法的过程,并不困难。
PowerShell只是一个工具而已,在Windows环境当中,使用PowerShell能让系统层面的自动化实现的更简单更顺手。
当然你也可以使用Python、Go甚至C#来达到自己的目的。:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值