powershell2
常用及重要命令汇总
get-help get-command show-command 更方便查看命令 get-alias
过虑行,过滤列,排序,格式化输出 where-object select-object sort-object format-table -autosize
======================
powershell命令
======================
cmdlet
Get-CimInstance
Get-Command
ForEach-Object
Get-Service
Get-Alias
Get-Content
Measure-Object
Sort-Object
---------------------
alias
measure
cat
=====================
cmd命令
=====================
PS C:\Users\fgy> tasklist /fi "imagename eq chrome.exe"
tasklist
find
winrm
cmd控制台与ps控制台
Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。 C:\Users\flt>powershell Windows PowerShell 版权所有 (C) 2009 Microsoft Corporation。保留所有权利。 PS C:\Users\flt>
Get-Partition|sort PartitionNumber ------------------------------------- Get-Alias|Where name -like "where" Get-Alias|where name -like "sort" Get-Alias|where name -like "cat"
Get-Alias g* ------------------------------------- Get-Process | Where-Object{$_.ProcessName -eq "svchost"} | Where-Object{$_.Id -gt 1000} 其实在PowerShell 3.0版本后Where就有了更简单的写法,我们可以甚至不需要大括号和”$_”符号 Get-Process|where ProcessName -like 'c*' |Format-Table -AutoSize Get-Process | where ProcessName -like 'c*' |select Id,processname|sort id Get-Process | Get-Member ------------------------------------- cat '.\new 1.txt'|measure -Character -Line -Word Get-Content C:\test.txt | Measure-Object -Character -Line -Word ------------------------------------- Get-PSDrive | Format-Table -autosize ------------------------------------- PS C:\Windows\system32> Get-Printer|Format-Table -AutoSize PS C:\Windows\system32> Get-PrinterDriver |Format-Table -autosize PS C:\Windows\system32> Get-PrinterPort|Format-Table -AutoSize
默认显示这三个字段 status service name display name of each service
Get-Service|Get-Member PS C:\Windows\system32> Get-Service a* PS C:\Windows\system32> Get-Service -DisplayName win* PS C:\Windows\system32> Get-Service |where status -like "running"|ft -AutoSize PS C:\Windows\system32> Get-Service|where {$_.status -eq "stopped"} PS C:\Windows\system32> "win*"|Get-Service Get-Service "s*" | Sort-Object status Get-Service "s*" | Sort-Object status -Descending
Get-Service|where status -like "runn*"|Measure-Object
PS C:\Windows\system32> $5=Get-Printer PS C:\Windows\system32> ForEach-Object {$5.name} Microsoft XPS Document Writer Microsoft Print to PDF Generic 26C-7SeriesPCL Fax
PS C:\Windows\system32> (10).gettype().name Int32 PS C:\Windows\system32> ('aa').gettype().name String
PS C:\Windows\system32> 1gb/20mb*10kb
PS C:\Windows\system32> 80kb*800*30/1gb
PS C:\Windows\system32> 10GB/(80KB*5)/30
PS C:\Users\Administrator> Get-Disk PS C:\Users\Administrator> Get-Partition PS C:\Users\Administrator> Get-Volume PS C:\Users\Administrator> Get-StorageSubSystem PS C:\Users\Administrator> Get-StorageFileServer PS C:\Users\Administrator> Get-FileShare
绑定双网卡 New-NetLbfoTeam "nic" -TeamingMode SwitchIndependent
Starting in Windows PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance Get-WmiObject Get-CimInstance
重定向 把命令的输出保存到文件中,‘>’为覆盖,’>>’追加 PS C:\Users\flt\ps> "powershell" > a.txt PS C:\Users\flt\ps> cat .\a.txt powershell PS C:\Users\flt\ps> "powershell" >>a.txt PS C:\Users\flt\ps> cat a.txt powershell powershell
模块
A module is a package that contains Windows PowerShell commands, such as cmdlets, providers, functions, workflows, variables, and aliases. 1、use Windows PowerShell modules 2、write Windows PowerShell modules A module is a package of commands get-commands中的source列
PS C:\Windows\system32> Get-Command -CommandType cmdlet -TotalCount 3 CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Export-ModuleMember 3.0.0.0 Microsoft.PowerShell.Core Cmdlet Get-Command 3.0.0.0 Microsoft.PowerShell.Core Cmdlet Get-Help 3.0.0.0 Microsoft.PowerShell.Core
PS C:\Windows\system32> Get-CimInstance -Query "SELECT * from Win32_Process WHERE name LIKE 'a%'" ProcessId Name HandleCount WorkingSetSize VirtualSize --------- ---- ----------- -------------- ----------- 1352 atiesrxx.exe 128 5074944 30732288 1508 atieclxx.exe 185 9027584 97927168 1876 AlibabaProtect.exe 545 33296384 128606208 4480 Apoint.exe 397 19505152 148824064 4820 ApMsgFwd.exe 69 4734976 50577408 924 ApntEx.exe 106 5890048 87404544
先看看$psversiontable,这个变量拆开来看就是ps-version-table,表示PowerShell中各组件的版本号列表。其中表示PowerShell自己的版本号(PSVersion),也包括.NET的版本号(CLRVersion),还有Windows版本号(BuildVersion) PS C:\Users\flt> $psversiontable Name Value ---- ----- PSVersion 4.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.18408 BuildVersion 6.3.9600.16406 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} PSRemotingProtocolVersion 2.2 接下来看看$host变量,里面一个Version,表示PowerShell的版本号。 PS C:\Users\flt> $host Name : ConsoleHost Version : 4.0 InstanceId : ec93e349-4833-4185-8f3d-8d365af04d70 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : zh-CN CurrentUICulture : zh-CN PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace PS C:\Users\flt> Get-Host Name : ConsoleHost Version : 4.0 InstanceId : ec93e349-4833-4185-8f3d-8d365af04d70 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : zh-CN CurrentUICulture : zh-CN PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace PS C:\Users\flt> Get-Host|Select-Object Name Name ---- ConsoleHost