PowerShell Beginner’s Guide 教程笔记
前言
PowerShell Beginner’s Guide 是 PowerShell 官方的入门教程。以下是我学习该教程时的笔记。
笔记
-
get-process命令。
Get-Process: Gets the processes that are running on the local computer or a remote computer.
需要注意的是这个命令在 5.1 版本,和 7.0 版本返回结果略有不同,5.1 版本多了 handles 这一对象。
可以通过指定名称的方式,获取指定进程信息。
# 14747 at SUPER-ALPHA in D:\shellCode [14:02:27] ➜ get-process -name wiz Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1114 82 41976 17184 36.55 25264 4 Wiz
多对象用逗号隔开即可:
# 14747 at SUPER-ALPHA in D:\shellCode [14:02:46] ➜ Get-Process -n wiz,tim Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1329 306 144016 91816 54.19 17136 4 TIM 1450 1056 256432 172324 658.11 21064 4 TIM 1114 82 42100 17272 36.59 25264 4 Wiz
-
Clear-Host 清屏操作(Clears the display in the host program)。它有两个别名(alias)
cls
和clear
。 -
Get-Alias
命令可以查看当前会话中的别名(Gets the aliases for the current session)。 -
cd $home
可以回到home
目录。 -
用
new-item
新建子目录或文件时,若已经存在同名文件会报错,这时可以用-force
强制执行:# 14747 at SUPER-ALPHA in D:\shellCode [13:20:02] ➜ New-Item -path temp1/fuckyou.txt -value "fuck you,asshole" Directory: D:\shellCode\temp1 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2019/9/5 13:22 16 fuckyou.txt # 14747 at SUPER-ALPHA in D:\shellCode [13:22:35] ➜ New-Item temp1/fuckyou.txt -value "fuck you,asshole" New-Item : The file 'D:\shellCode\temp1\fuckyou.txt' already exists. At line:1 char:1 + New-Item temp1/fuckyou.txt -value "fuck you,asshole" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (D:\shellCode\temp1\fuckyou.txt:String) [New-Item], IOException + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand # 14747 at SUPER-ALPHA in D:\shellCode [13:22:59] ➜ New-Item temp1/fuckyou.txt -value "fuck you,asshole" -force -a--- 2019/9/5 13:23 16 fuckyou.txt
-
set-content
命令也可以设置文件内容,若文件不存在会自动创建文件,但是上级的文件夹不会自动创建。# 14747 at SUPER-ALPHA in D:\shellCode [13:36:03] ➜ set-content temp1\new.txt -value "fuck you" # 14747 at SUPER-ALPHA in D:\shellCode [13:36:11] ➜ ls temp1 -a--- 2019/8/30 19:00 38 another.txt -a--- 2019/9/5 13:35 17 fuckyou.txt -a--- 2019/8/19 1:34 11.86KB mytest.html -a--- 2019/9/5 13:36 10 new.txt -a--- 2019/8/30 18:49 10 test1.txt -a--- 2019/9/4 15:15 0 test11.txt -a--- 2019/9/4 15:15 0 test12.txt -a--- 2019/8/30 18:50 11 test2.txt -a--- 2019/8/30 18:58 1.79KB test3.txt -a--- 2019/8/30 19:00 38 test4.txt # 14747 at SU