首页 > 技术文章 > PowerShell 基础命令

cnsealine 2013-09-22 21:12 原文

获取系统日志  Get-EventLog -LogName System -EntryType Error -After 2013-9-18

查看打开PowerShell的时间  (Get-Process -pid $pid).starttime

判断今天星期几  (Get-Date).DayOfWeek

判断某年是否为润年  [datetime]::isleapyear(2013)

  [DateTime]是引用一个日期和时间类型,双冒号(::)在PowerShell中是调用静态方法的意思。IsLeapYear()函数用于判断闰年、平年。参数直接以数字的形式给函数即可。

 查看IPv4地址  foreach($ip in(ipconfig) -like '*IPv4*'){($ip -split ':')[1]}

还有多少天元旦  ([DateTime]"1/1/2014" -[datetime]::now).days

关机  1.使用Win32_OperatingSystem类的Win32Shutdown方法 2.使用Stop-Computer 跟 Restart-Computer cmdlet

$strComputer = "."  
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer  
$OS.psbase.Scope.Options.EnablePrivileges = $true  
$OS.win32shutdown(1)  

 

获取IP地址  [net.dns]::GetHostAddresses('') | select -ExpandProperty IPAddressToString

三种方法关进程  Stop-Process -name notepad; Get-Process notepad | Stop-Process; (Get-Process notepad).Kill()

Get Process commandline information  gwmi Win32_Process -Filter "Name='chrome.exe'" | Select-Object CommandLine  注意这里的规范

重排  gwmi Win32_Process | Select-Object @{Name="Hello";Expression={$_.name}}

下载  (New-Object System.Net.WebClient).DownloadFile($source, $destination)

    $source="http://www.xiazai.com/dianying.zip"  $destination="c:\temp\2.zip"

查寻磁盘大小   gwmi -query "Select * from Win32_LogicalDisk Where DriveType = '3'"

 查寻主板序列号(下载驱动用)  (gwmi Win32_BaseBoard).SerialNumber  对华硕:(gwmi Win32_BIOS).SerialNumber

进程按启动时间排序  gwmi Win32_Process | sort-object CreationDate | select-object path

推荐阅读