PowerShell常用命令
1.1查看PowerShell的版本
1.1.1 $PSVersionTable.PSVersion
- Click Start, click All Programs, click Accessories, click Windows PowerShell, and then click Windows PowerShell.
- In the Windows PowerShell console, type the following command at the command prompt and then press ENTER:
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
1.1.2 $PSVersionTable
PS C:\Users\clu> $PSVersionTable
Name Value
---- -----
PSVersion 5.0.10586.1045
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.1045
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
https://github.com/PowerShell/PowerShell/releases
PS C:\Program Files\PowerShell\6.0.0-beta.6> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.0-beta
PSEdition Core
GitCommitId v6.0.0-beta.6
OS Microsoft Windows 10.0.10586
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
1.2
PS C:\Users\Administrator> get-host
Name : ConsoleHost
Version : 2.0
InstanceId : 46ec5108-c784-4098-856e-eb1345f2eb86
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
1.3查看操作系统
PS C:\Users\Administrator> wmic os get caption
Caption
Microsoft Windows 7 Ultimate
1.4查看架构
PS C:\Users\Administrator> wmic os get osarchitecture
OSArchitecture
64-bit
1.5 查找Service,
format-table可以确保Name被完整显示
1.5.1 get-service | format-table servicename,displayname -autosize
查找以L开头的服务
Status Name DisplayName
------ ---- -----------
Running LanmanServer Server
Running LanmanWorkstation Workstation
Running LFXSVC_FileExch... LisaFileExchange5
Stopped LFXSVC_FileExch... FileExchangeService1
Stopped lltdsvc Link-Layer Topology Discovery Mapper
Running lmhosts TCP/IP NetBIOS Helper
1.5.2 Get-Service | Where-Object {$_.displayName.Contains("File")} | Select name,DisplayName
可以筛选出两列数据,列名用逗号分隔
1.6 查看环境变量
[environment]::ExpandEnvironmentVariables("%HomeDrive%%HomePath%")
[environment]::ExpandEnvironmentVariables("%Home%")
1.7查看文件内容并进行筛选
get-content updatelist.txt | findstr "29849"
http://support.microsoft.com/?kbid=2984972 WASYGSHA01-1050 Security Update KB2984972 WASYGSHA01-1050\adm-bchen 5/9/2017
1.8查找字符串
findstr
或者可以给此命令起一个别名grep来使用
PS) new-alias grep findstr
PS) C:\WINDOWS> ls | grep -I -N exe
1.9 电脑关机和重启
Stop-Computer
Restart-Computer
1.10 时间相关的操作
PS C:\> Set-Date -Date (Get-Date).AddDays(3)
1.11在后台打开进程
Start-Process .\HeroesSwitcher_x64.exe -WindowStyle Hidden
1.12 查看文件hash (sha 256)
PS D:\ChuckLu\ProgramFiles> Get-FileHash -Path .\flashplayer28_xa_install.exe | Format-List
Algorithm : SHA256
Hash : C3AD1DD42801ADA280E42EB3E4F5D00FD402F3B0213FEE4C467FB2A8F1DED88C
Path : D:\ChuckLu\ProgramFiles\flashplayer28_xa_install.exe
PS D:\ChuckLu\ProgramFiles> Get-FileHash -Path .\flashplayer28_xa_install.exe -Algorithm SHA1 | Format-List
Algorithm : SHA1
Hash : F35F8093A98752B48F5D9FCDF5A57A6C23558174
Path : D:\ChuckLu\ProgramFiles\flashplayer28_xa_install.exe
1.13 获取电脑信息
1.13.1 查看computer name
Get-ComputerInfo -Property "CsName" | Format-List
1.14 location
1.14.1 Push-Location
Adds the current location to the top of a location stack.
1.14.2 Pop-Location
Changes the current location to the location most recently pushed onto the stack.
1.15 get the key of parameters of a specified command
((Get-Command -Name Start-Resgen).Parameters).Keys
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
1.16 New-Item
在当前目录下创建一个文件
New-Item post-build.bat
1.16 Get the absolute path of current location
(Get-Item .).FullName
https://github.com/chucklu/Scripts/blob/master/Powershell/chuck.psm1
获取cpu信息
~\Desktop> Get-WMIObject win32_Processor
Caption : Intel64 Family 6 Model 94 Stepping 3
DeviceID : CPU0
Manufacturer : GenuineIntel
MaxClockSpeed : 2701
Name : Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
SocketDesignation : U3E1
~\Desktop> Get-WmiObject –class Win32_processor | ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessors, Addresswidth
SystemName Name DeviceID NumberOfCores NumberOfLogicalProcessors AddressWidth
---------- ---- -------- ------------- ------------------------- ------------
WASYGSHA01-1020 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz CPU0 4 8 64
获取显卡信息
~\Desktop> Get-WmiObject Win32_VideoController
RunspaceId : 06ebe556-7468-47ce-a9de-9232a7b51ad7
__GENUS : 2
__CLASS : Win32_VideoController
__SUPERCLASS : CIM_PCVideoController
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_VideoController.DeviceID="VideoController1"
__PROPERTY_COUNT : 59
__DERIVATION : {CIM_PCVideoController, CIM_VideoController, CIM_Controller, CIM_LogicalDevice...}
__SERVER : WASYGSHA01-1020
__NAMESPACE : root\cimv2
__PATH : \\WASYGSHA01-1020\root\cimv2:Win32_VideoController.DeviceID="VideoController1"
AcceleratorCapabilities :
AdapterCompatibility : Intel Corporation
AdapterDACType : Internal
AdapterRAM : 1073741824
Availability : 3
CapabilityDescriptions :
Caption : Intel(R) HD Graphics 530
ColorTableEntries :
ConfigManagerErrorCode : 0
ConfigManagerUserConfig : False
CreationClassName : Win32_VideoController
CurrentBitsPerPixel : 32
CurrentHorizontalResolution : 1920
CurrentNumberOfColors : 4294967296
CurrentNumberOfColumns : 0
CurrentNumberOfRows : 0
CurrentRefreshRate : 60
CurrentScanMode : 4
CurrentVerticalResolution : 1080
Description : Intel(R) HD Graphics 530
DeviceID : VideoController1
DeviceSpecificPens :
DitherType : 0
DriverDate : 20160929000000.000000-000
DriverVersion : 20.19.15.4531
ErrorCleared :
ErrorDescription :
ICMIntent :
ICMMethod :
InfFilename : oem7.inf
InfSection : iSKLD_w10
InstallDate :
InstalledDisplayDrivers : igdumdim64.dll,igd10iumd64.dll,igd10iumd64.dll,igd12umd64.dll
LastErrorCode :
MaxMemorySupported :
MaxNumberControlled :
MaxRefreshRate : 60
MinRefreshRate : 48
Monochrome : False
Name : Intel(R) HD Graphics 530
NumberOfColorPlanes :
NumberOfVideoPages :
PNPDeviceID : PCI\VEN_8086&DEV_191B&SUBSYS_06DE1028&REV_06\3&11583659&1&10
PowerManagementCapabilities :
PowerManagementSupported :
ProtocolSupported :
ReservedSystemPaletteEntries :
SpecificationVersion :
Status : OK
StatusInfo :
SystemCreationClassName : Win32_ComputerSystem
SystemName : WASYGSHA01-1020
SystemPaletteEntries :
TimeOfLastReset :
VideoArchitecture : 5
VideoMemoryType : 2
VideoMode :
VideoModeDescription : 1920 x 1080 x 4294967296 colors
VideoProcessor : Intel(R) HD Graphics Family