PowerShell
1.0 > 5.0
get-service -name xxx
start-service/stop-service
get-disk
get-physicaldisk
get-verb:获取所有的动词。所有动词都必须在这个 verb 列表里面。
get-command: 获取所有命令。
get-alias: 获取别名
$psversiontable: 获取 powershell 版本
PS C:\> Get-Process | where {$_.Name -like "*fox"} | ConvertTo-Html > d:\ps.html 找到名称为 *fox 的进程并且给出信息。
PS C:\Windows\system32> $host
Name : ConsoleHost
Version : 4.0
InstanceId : b02ba506-705c-4027-9a0a-e6f9f8481a0b
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
get-vm -name "01","02","03" | remove-vm
get-help
get-help full
update-help
save-help -destinationpath c:\help\
Restart-computer -whatif
PS C:\Windows\system32> (Get-Service)[0] |Select-Object *
(数组形式,返回第一个值)
Name : AdobeARMservice
RequiredServices : {}
CanPauseAndContinue : False
CanShutdown : False
CanStop : False
DisplayName : Adobe Acrobat Update Service
DependentServices : {}
MachineName : .
ServiceName : AdobeARMservice
ServicesDependedOn : {}
ServiceHandle : SafeServiceHandle
Status : Stopped
ServiceType : Win32OwnProcess
Site :
Container :
PS C:\Windows\system32> (Get-Service)[0] |Select-Object name
Name
----
AdobeARMservice
PS C:\Windows\system32> (Get-Service)[0] |Select-Object name,status
Name Status
---- ------
AdobeARMservice Stopped
PS C:\Windows\system32> (Get-Service)[0] |Select-Object name,status | ft -AutoSize
Name Status
---- ------
AdobeARMservice Stopped
PS C:\Windows\system32> Get-Service| where {$_.Status -match 'stopped' }| where {$_.Name -like 'WP*' }| Select-Object Name
Name
----
WPCSvc
WPDBusEnum
赋值 service 的 Name 给一个变量:
PS C:\Users\allen> $var=(Get-Service)[0]
PS C:\Users\allen> write-host $var
AdobeARMservice
PS C:\Users\allen> $var=(Get-Service)[0] | Select-Object Name
PS C:\Users\allen> echo $var
Name
----
AdobeARMservice
PS C:\Users\allen> write-host $var
@{Name=AdobeARMservice} // 这是一个 hashtable/dictionary
PS C:\Users\allen> write-host $var.Name
AdobeARMservice
下面是以对象的方式直接找到 Name 属性为 'WP*' 的 service
(Get-Service).Name -like 'WP*'
WPCSvc
WPDBusEnum
变量:
$var = "1","2","3"
echo $var
1
2
3
$var = 1,2,3,4
$new = $var.clone()
echo $new
1
2
3
4
Get-Service | where {$_.Name -eq 'WinRM'} | Start-Service (启动 WinRM 服务)
Get-Service | where {$_.Name -eq 'WinRM'} | Select-Object Name,Status | ft -AutoSize (显示该服务当前状态)
或者简写为:
Get-Service WinRM | Stop-Service
Get-Service WinRM
或者用变量:
$WinRM = Get-Service WinRM
$WinRM | Stop-Service
$WinRM
或者调用方法:
$WinRM = Get-Service WinRM
$WinRM.Start()
$WinRM