你应该选择 Powershell 的10个理由(抛弃 cmd)

所属分类: 脚本专栏 / PowerShell 阅读数: 2068
收藏 0 赞 0 分享

Powershell 从 Windows7 时代开始内置于 Windows 系统当中,可以看作是微软对 cmd 的大升级,目前两者并存于 Windows 系统中。

首先强烈推荐一个 powershell 学习网站:

powershell在线教程

1. powershell 更加 “powerful”

微软起“powershell”这个名字虽然有吹牛逼的嫌疑(我以前也这样想),但从事实来看并非如此。powershell 的强大程度分分钟秒杀 Linux 上的 bash,更不要说饱受诟病的 cmd 了。(无意打广告)

2. powershell 全面支持面向对象

powershell 背后依靠的是一套完整的 .NET 编程体系,其脚本更容易编写且稳健性大大提升。反过来看 cmd,那些完全由各种命令堆砌而成的一条条指令简直就是“小打小闹”。

3. 从 cmd 迁移到 powershell 成本几乎为零

如果你对老朋友 cmd 充满情怀,无法舍弃,完全无妨!因为 powershell 可以看作 cmd 的超集,所有的常用命令诸如dir, cd, ipconfig等在 powershell 中都能直接使用。但背后的实现方式是完全不同的,powershell 基于完全的面向对象,它通过给函数和对象“起别名”的方式来支持这些旧的命令。

4. 诱人的管道操作

管道操作的灵感来自 Linux 的 shell,但由于 powershell 将一切都包装成为对象,而不是直接处理字符串,因此其管道操作的灵活程度远在 Linux 的 shell 之上。

例如:

PS C:\Users\Haley> ls | sort -Descending Name | Format-Table Name,Mode

Name              Mode
----              ----
VirtualBox VMs         d-----
Videos             d-r---
Searches            d-r---
Saved Games          d-r---
Pictures            d-r---
OneDrive            d-r---
Music             d-r---
Links             d-r---

这条命令列出当前路径下的所有文件,按照名称降序排序,并以表格的形式输出,且只显示Name和Mode两个字段。

5. 绝对完备的帮助文档

任何函数与对象都能够通过help *命令来查看其帮助文档(准确来说应该是Get-Help函数,这是更加“面向对象”化的命名方式,而help是它的别名),如果看不明白,加上-examples参数会有应用实例,如果仍看不明白,加上-online参数会打开完整的在线帮助文档,不得不佩服,微软的一条龙服务做的很到位。

例如,关于ls的帮助文档如下:

PS C:\Users\Haley> help ls

名称
  Get-ChildItem

摘要
  Gets the items and child items in one or more specified locations.


语法
  Get-ChildItem [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device | Normal
   | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream |
   NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden] [-Include <String[]>
  ] -LiteralPath <String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>]

  Get-ChildItem [[-Path] <String[]>] [[-Filter] <String>] [-Attributes {ReadOnly | Hidden | System | Directory | Arch
  ive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypte
  d | IntegrityStream | NoScrubData}] [-Depth <UInt32>] [-Directory] [-Exclude <String[]>] [-File] [-Force] [-Hidden]
   [-Include <String[]>] [-Name] [-ReadOnly] [-Recurse] [-System] [-UseTransaction] [<CommonParameters>]


说明
  The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the
   items inside the container, known as child items. You can use the Recurse parameter to get items in all child cont
  ainers.

  A location can be a file system location, such as a directory, or a location exposed by a different Windows PowerSh
  ell provider, such as a registry hive or a certificate store.


相关链接
  Online Version: http://go.microsoft.com/fwlink/?LinkId=821580
  Get-Item
  Get-Location
  Get-Process
  Get-PSProvider

备注
  若要查看示例,请键入: "get-help Get-ChildItem -examples".
  有关详细信息,请键入: "get-help Get-ChildItem -detailed".
  若要获取技术信息,请键入: "get-help Get-ChildItem -full".
  有关在线帮助,请键入: "get-help Get-ChildItem -online"

6. 支持基本的数学运算和数组操作(加减乘除模),打开就能当计算器用

PS C:\Users\Haley> 1 + 1
2
PS C:\Users\Haley> 3 * 5
15
PS C:\Users\Haley> 0xab
171
PS C:\Users\Haley> 1kb
1024
PS C:\Users\Haley> 1mb / 1kb
1024

7. 良好的脚本编程体验

powershell 脚本的语法与高级编程语言非常相近,例如其分支语句if(...){} else{}、循环语句for(...){}与 C 语言别无二致,因而大大提高了编程体验。

8. 拥有丰富的字符串操作对象和函数,全面支持正则表达式

字符串处理是 shell 脚本的主要任务,正则表达式的重要性也无需多说。

9. 支持重定向,读写文件易如反掌

powershell 原生支持将结果导出到html, csv, xml等文件,也可以通过重定向从文件中读取内容。

例如:

PS C:\Users\Haley> ls | Select-Object Mode,Name | Export-Csv ~/desktop/test.csv

这样就把当前目录下的所有 文件名-读写权限 保存到了一个 csv 文件中。

10. 支持 Debug

虽然这个功能很少用到,但支持 debug 是走向完备编程语言不可或缺的一项功能。

更多精彩内容其他人还在看

PowerShell隐藏不显示窗口的多种方法

这篇文章主要介绍了PowerShell隐藏不显示窗口的多种方法,本文讲解了启动PowerShell时隐藏自己的窗口、在PowerShell启动其它进程时隐藏窗口、使用PowerShell隐藏其它进程的窗口三种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891

这篇文章主要介绍了PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891,本文给出了详细的排查步骤和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell目录文件夹管理权限的继承和指定方法

这篇文章主要介绍了Powershell目录文件夹管理权限的继承和指定方法,本文给出了创建文件夹、获取当前权限、添加新的权限、添加管理员权限等,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell查看本机文件关联程序和默认打开程序的方法

这篇文章主要介绍了PowerShell查看本机文件关联程序和默认打开程序的方法,本文给出了查看方法,同时给出了一份读取结果,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell脚本反引号用法实例:随时随地给代码换行

这篇文章主要介绍了PowerShell脚本反引号用法实例:随时随地给代码换行,在遇到一些超长代码行时非常有用,一般编程代码一行的字符数不超过80个哦,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 数组的多种录入方法

这篇文章主要介绍了PowerShell 数组的多种录入方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell String对象方法小结

这篇文章主要介绍了PowerShell String对象方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用PowerShell获取当前主机内存使用量和总量的方法

这篇文章主要介绍了使用PowerShell获取当前主机内存使用量和总量的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell批量修改AD用户密码属性的代码

这篇文章主要介绍了PowerShell批量修改AD用户密码属性的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 自动备份oracle并上传到ftp

我这里有这样一个需求:有一个数据库,每天使用SQL Server Agent自动生成备份文件。然后,这个数据库非常重要,需要把每天的备份上传一个远程的FTP服务器上去。下面我们来看看如何使用Powershell来实现吧
收藏 0 赞 0 分享
查看更多