Windows Tips
Table of Contents
1. Powershell
Powershell 是 Windows 中一个强大的 Shell 工具。
1.1. 查看进程
执行 Get-Process
(或者 ps
)可以查看所有进程,如:
PS C:\Users\user1> ps Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 145 8 1880 6508 8.27 3796 0 AdminService 90 6 1592 5804 0.38 5284 0 AggregatorHost 358 21 13688 27540 0.13 11232 1 ApplicationFrameHost 332 30 8576 1968 0.03 2348 1 backgroundTaskHost 261 28 8192 22908 0.09 10488 1 backgroundTaskHost 138 9 1504 7804 0.02 7628 1 ChsIME 95 7 1116 5884 0.30 1880 0 conhost 259 14 7812 18812 98.67 3124 1 conhost 81 7 5288 5332 0.02 4692 0 conhost 675 26 2128 5784 4.63 908 0 csrss 413 19 3068 6552 8.86 1000 1 csrss 685 31 10352 28036 0.77 6824 1 ctfmon 372 18 4088 12176 0.14 4348 0 dasHost ......
下面是对进程进行排序或者过滤的一些例子:
Get-Process | Sort-Object -Descending WS # 按内存排序 Get-Process | Sort-Object -Descending CPU # 按 CPU 排序 Get-Process | Where-Object {$_.WorkingSet -gt 20000000} # 按条件过滤显示
1.1.1. 查看进程所完整 Path
输出正在运行的进程其对应可执行程序的完整 Path:
Get-Process | ForEach-Object {$_.Path} ps | % {$_.Path}
PS C:\Users\user1> Get-Process | ForEach-Object {$_.Path} C:\WINDOWS\System32\drivers\AdminService.exe C:\WINDOWS\System32\AggregatorHost.exe C:\WINDOWS\system32\ApplicationFrameHost.exe C:\Windows\system32\backgroundTaskHost.exe C:\Windows\System32\InputMethod\CHS\ChsIME.exe C:\WINDOWS\system32\conhost.exe C:\WINDOWS\system32\conhost.exe C:\WINDOWS\system32\conhost.exe C:\WINDOWS\system32\ctfmon.exe C:\WINDOWS\system32\dashost.exe C:\Program Files\Dell\DellDataVault\DDVCollectorSvcApi.exe C:\Program Files\Dell\DellDataVault\DDVDataCollector.exe C:\Program Files\Dell\DellDataVault\DDVRulesProcessor.exe ......
1.1.2. 按 CPU 使用时间持续监控进程
PS C:\Users\user1> While(1) {ps | sort -des cpu | select -first 10 | ft -a; sleep 1; cls} Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 5874 434 8147492 8221960 23,957.38 8316 1 myprogram 4422 0 204 3996 2,229.41 4 0 System 847 98 355224 270540 781.83 4116 0 MsMpEng 335 69 69536 39452 340.44 7780 0 TiWorker 261 14 14136 23836 287.44 9140 1 conhost 882 115 109696 42720 133.66 12720 0 svchost 1164 127 88264 34344 130.34 5272 0 DellSupportAssistRemedationService 223 12 2612 6544 103.66 2212 0 svchost 794 66 140420 98828 93.45 5744 0 Dsapi 1063 43 36088 23196 86.00 1396 1 dwm
ft
是 Format-Table
的缩写, -a
是 -autosize
的缩写。从上面输出中,可以发现 myprogram 消耗的 CPU 时间最多。
1.1.3. 查看某端口被哪个进程占用
下面命令可以查看端口 5900 被哪个进程占用:
PS C:\Users\user1> Get-NetTCPConnection | where Localport -eq 5900 | select Localport,OwningProcess Localport OwningProcess --------- ------------- 5900 640
1.2. 管理 Service
执行 Get-Service
可以列出系统中的所有 Service,如:
PS C:\Users\user1> Get-Service Status Name DisplayName ------ ---- ----------- Stopped AarSvc_59358 Agent Activation Runtime_59358 Stopped AJRouter AllJoyn Router Service Stopped ALG Application Layer Gateway Service Stopped AppIDSvc Application Identity Running Appinfo Application Information Stopped AppReadiness App Readiness Running AppXSvc AppX Deployment Service (AppXSVC) Running AtherosSvc AtherosSvc Running AudioEndpointBu... Windows Audio Endpoint Builder Running Audiosrv Windows Audio Stopped autotimesvc 手机网络时间 Stopped AxInstSV ActiveX Installer (AxInstSV) Stopped BcastDVRUserSer... GameDVR 和广播用户服务_59358 Stopped BDESVC BitLocker Drive Encryption Service Running BFE Base Filtering Engine ......
要对 Service 进行管理,可以执行下面命令:
Start-Service -name myservicename Restart-Service -name myservicename Stop-Service -name myservicename
1.3. 查看 CPU 信息
查看 CPU 核的数量:
PS C:\Users\user1> (Get-ComputerInfo -Property CsProcessors).CsProcessors Name : Intel(R) Core(TM) i7-10700T CPU @ 2.00GHz Manufacturer : GenuineIntel Description : Intel64 Family 6 Model 165 Stepping 5 Architecture : x64 AddressWidth : 64 DataWidth : 64 MaxClockSpeed : 1992 CurrentClockSpeed : 1992 NumberOfCores : 8 NumberOfLogicalProcessors : 16 ProcessorID : BFEBFBFF000A0655 SocketDesignation : U3E1 ProcessorType : CentralProcessor Role : CPU Status : OK CpuStatus : Enabled Availability : RunningOrFullPower
查看 CPU 型号:
PS C:\Users\user1> Get-WmiObject Win32_Processor Caption : Intel64 Family 6 Model 165 Stepping 5 DeviceID : CPU0 Manufacturer : GenuineIntel MaxClockSpeed : 1992 Name : Intel(R) Core(TM) i7-10700T CPU @ 2.00GHz SocketDesignation : U3E1
1.4. 查看磁盘信息
查看每个盘的信息(可用空间和总空间):
PS C:\Users\user1> get-wmiobject -class win32_logicaldisk DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 703022530560 Size : 1021349466112 VolumeName : OS
查看磁盘信息(比如品牌,大小等):
PS C:\Users\user1> wmic diskdrive get name,model,size,status Model Name Size Status INTEL SSDPEKNW010T8 \\.\PHYSICALDRIVE0 1024203640320 OK
1.5. 查看目录中所有文件大小
查看某目录及其子目录中文件的大小:
PS C:\Users\user1> Get-ChildItem -Recurse C:\msys64\ | Measure-Object -Property Length -Sum Count : 70446 Average : Sum : 2650672628 Maximum : Minimum : Property : Length
如果想以 Mb 为单位显示大小,可以这样:
PS C:\Users\user1> (Get-ChildItem -Recurse C:\msys64\ | Measure-Object -Property Length -Sum).Sum / 1Mb 2527.87840652466
1.6. 下载文件
使用 Invoke-WebRequest
可以下载文件。如:
PS C:\Users\user1> Invoke-WebRequest -Uri https://golang.org/dl/go1.16.7.windows-amd64.zip -OutFile go1.16.7.windows-amd64.zip
1.7. 解压压缩文件
解压 zip 文件到当前目录:
PS C:\Users\user1> Expand-Archive -Path .\go1.16.7.windows-amd64.zip -DestinationPath .\
1.8. 查看环境变量
执行 [Environment]::GetEnvironmentVariable("Path")
或者 $env:Path
可以查看 Path 环境变量,如:
PS C:\Users\user1> $env:Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Users\user1\AppData\Local\Microsoft\WindowsApps;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
1.9. 设置 SSH 登录的默认 Shell 为 Powershell
Windows 10 中内置了 OpenSSH 服务,通过 SSH 客户端连接到服务器后,默认启动的 Shell 是 cmd.exe。可以通过修改注册表设置默认的 Shell 为 powershell.exe:
PS C:\Users\user1> New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
参考:https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration
1.10. 查看 Powershell 版本
执行 $PSVersionTable
查看版本号:
PS C:\Users\user1> $PSVersionTable Name Value ---- ----- PSVersion 5.1.19041.1 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
2. 包管理工具(choco)
choco 是 Windows 中的一个包管理工具。
安装 choco:
PS C:\Users\user1> Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
使用 choco 安装其它软件(如 vim):
PS C:\Users\user1> C:\ProgramData\chocolatey\bin\choco.exe install vim
列出通过 choco 安装到本地系统的所有软件:
PS C:\Users\user1> choco list --local-only
注:winget 是 Microsoft 提供的包管理工具,但在使用 ssh 登录环境中却无法使用,这个问题参见 https://github.com/microsoft/winget-cli/issues/1474
3. MSYS2(软件编译环境)
MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.
3.1. MSYS2 VS. Cygwin
MSYS2 编译出来的程序是 Native 程序。Cygwin 编译的程序依赖于 Cygwin 提供的兼容层。
3.2. 安装 MSYS2
首先在 MSYS2 官网下载安装包,比如:https://github.com/msys2/msys2-installer/releases/download/2021-07-25/msys2-x86_64-20210725.exe
双击安装包即可进行安装。下面介绍如何在 terminal 中安装,执行下面命令即可:
> .\msys2-x86_64-20210725.exe --root C:\msys64 install
在 terminal 中启动 shell 环境(下面是使用 mingw64 环境):
> C:/msys64/msys2_shell.cmd -defterm -no-start -mingw64
3.3. 5 种 shell 环境
一共有 5 种 shell 类型可以选择:
mingw32 mingw64 ucrt64 clang64 msys
同一个软件包,也有各种环境的版本,分别在不同目录中,如:
# pacman -F pcre.h mingw32/mingw-w64-i686-pcre 8.45-1 mingw32/include/pcre.h mingw64/mingw-w64-x86_64-pcre 8.45-1 mingw64/include/pcre.h ucrt64/mingw-w64-ucrt-x86_64-pcre 8.45-1 ucrt64/include/pcre.h clang64/mingw-w64-clang-x86_64-pcre 8.45-1 clang64/include/pcre.h msys/pcre-devel 8.45-1 (development) usr/include/pcre.h
3.4. MSYS2 包管理软件(pacman)
MSYS2 使用 pacman(这个工具移植于 Arch Linux)来安装软件,比如:
$ pacman -Syu # 更新 $ pacman -S --needed base-devel mingw-w64-x86_64-toolchain # 安装 make, gcc 等等
表 1 是 pacman 的基本用法。
命令 | 说明 |
---|---|
pacman -S XXX | 安装软件 |
pacman -Rs XXX | 卸载软件 |
pacman -Ql XXX | 查询某软件包安装了哪些文件到系统中 |
pacman -Fy pcre.h | 查询哪个软件包可以提供某文件(比如 pcre.h) |
3.5. Tips
3.5.1. 查看 Windows 目录对应位置
使用 cygpath 可以查看 Windows 目录对应位置,如:
$ cygpath -u C:\\foo /c/foo $ cygpath -H /c/Users