PowerShell中终止管道的方法
如果你能够提前知道你想从管道中获取元素的个数,可以使用Select-Object来终止流处理命令的的管道,这会节省很多时间。
下面的例子会从Windows文件夹下搜寻 explorer.exe示例,因为Select-Object语句可以在管道找到结果的那一瞬终止管道。否则,Get-ChildItem会递归的遍历所有子目录然后匹配出你想要的结果。
#requires -Version 3Get-ChildItem -Path c:Windows -Recurse -Filter explorer.exe -ErrorAction SilentlyContinue | Select-Object -First 1
注意Select-Object可以终止流处理命令管道的这个功能,是在PowerShell 3.0以后才引入的。在早期的windows powershell版本中,select -first 1 会得到所有结果,然后再取第一个,很傻的模式吧!
PowerShel程序执行完后删除脚本自身的方法
当脚本执行完成后,可以删除自身#删除脚本自身remove-item$MyInvocation.MyCommand.Path-force
Powershell比较两个文件夹的不同
快速找出两个文件夹中不同的部分,可以使用下面例子:$list1=Get-ChildItemc:Windowssystem32|Sort-Object-PropertyName$list2=Get-ChildItem\server12c$windowssystem32|Sort-Objec
Powershell实现加密解密文本文件方法实例
适用于Powershell3.0及以后版本。假设你需要给文件加密,下面教你如何给自己的文件加密:$Path="$env:tempsecret.txt"$Secret='HelloWorld!'$Passphrase='Somesecretkey'$key=[