第二章 Interactive PowerShell
作为计算器
+ - * / :分别代表加减乘除
() : 总是先计算括号中的内容
. :十进制分隔符
例如:
(Dir *.txt).Count (Enter)
12
, : 逗号在PowerShell中是特殊的,它直接将逗号前后元素组成数组 例如:
4,3 +2 (Enter)
4
3
2
GB/MB/KB :GB等于数字乘以1024*1024*1024,mb等于数字乘以1024*1024 (数字与MB之间不能有
4GB/720MB (Enter)
5.6888888888889
1mb (Enter)
1048576
0x开头 :表示16进制
-----执行外部命令-----
Ipconfig : 跟传统的CMD效果一样
Tracert :检查一个web site是不是online
PS C:\Users\LIHUANG\Desktop\common> tracert powershell.com
Tracing route to powershell.com [65.38.114.170]
over a maximum of 30 hops:
1 2 ms 1 ms 1 ms 16.187.84.3
2 1 ms <1 ms <1 ms 16.160.220.181
3 1 ms 1 ms 1 ms 16.160.222.153
4 5 ms 2 ms 1 ms clj01gwn21-cw-hp.asiapac.hp.net [15.148.131.54]
5 * 71 ms 63 ms hpm01gwb21-cw-hp.asiapac.hp.net [15.148.129.122]
6 64 ms 64 ms 64 ms hpm01gwb21-hp-cw.asiapac.hp.net [15.148.129.121]
7 64 ms 63 ms 63 ms 16.160.15.45
8 * * * Request timed out.
9 * * * Request timed out.
10 * * * Request timed out.
11
quit :用于退出text-based 命令,比如irb 进入ruby编译器
cls :清空当前屏幕
cmd /c help :获得cmd下的命令以及简介
cmd /c help command:获得command的介绍
安全限制
不能直接执行 wordpad 这样的命令,必须制定它的路径。(否则ps会到$PATH路径下找)
你也不可以执行C:\programs\Windows NT\accessories\wordpad.exe ,因为路径中有空格
你需要把它用双引号引起来,然后用& 执行它
& "C:\programs\Windows NT\accessories\wordpad.exe"
或者你可以先切换路径,然后再执行
Cd "C:\programs\Windows NT\accessories"
wordpad.exe
The term "wordpad" is not recognized as a cmdlet,
function, operable program or script file.
Verify the term and try again.
At line:1 char:11
+ wordpad.exe <<<<
但是你仍然会遇到错误,因为ps需要相对路径或者绝对路径你需要使用
.\wordpad.exe
安全路径
$env:Path : 输出环境变量$path的内容
如果运行的命令在$path路径下,则不需要显示的制定运行路径。
你可以随时添加路径:
$env:path +=";C:\programs\Windows NT\accessories"
这样就可以直接运行wordpad.exe了
但要注意,此次改动仅当ps还在运行的时候有效。它会随着ps的关闭而失效。如果你想保留这些自定义设置,还需要做另外的操作,第十章会讲到。
如果你要输入一个非常长的路径名,别忘了‘TAB’, 它能自动帮你填充可能的选项。如果路径中有空格,‘TAB’会自动用双引号包裹路径名,并在最前面加上&符号。
---------Cmdlets------------
ps自带的内部命令叫做Cmdlets, 你可以检索所有的cmdlets :
Get-Command -commandTypecmdlet
cmdlets根据动作可以分为:
Action | Discription |
Add | 添加 |
Clear | 清除 |
Compare | 比较 |
Convert | 转换 |
Copy | 拷贝 |
Export | 导出 |
Format | 格式化 |
Get | 获取 |
Group | 分组 |
Import | 导入 |
Measure | 测量 |
Move | 移动 |
New | 新建 |
Out | 输入 |
Read | 读入 |
Remove | 删除 |
Rename | 重命名 |
Resolve | 解析 |
Resume | 恢复 |
Select | 选择 |
Set | 设置 |
Sort | 排序 |
Split | 分隔 |
Start | 开始 |
Stop | 关闭/暂停 |
Suspend | 挂起 |
Tee | 分开 |
Test | 测试 |
Trace | 跟踪 |
Update | 更新 |
有任何问题可以使用
Get-Help Get-Commond-detailed
或者
Get-Command *help*-CommandType cmdlet
命令的参数
例: 要查看 Get-ChildItem 的参数,使用 get-help get-childItem :PS C:\Users\LIHUANG\Desktop\common> Get-Help Get-ChildItem
NAME
Get-ChildItem
SYNTAX
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse]
[-Force] [-Name] [-UseTransaction] [-Attributes <FlagsExpression[FileAttributes]> {ReadOnly | Hidden | System |
Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline |
NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}] [-Directory] [-File] [-Hidden] [-ReadOnly]
[-System] [<CommonParameters>]
Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>] [-Exclude <string[]>] [-Recurse]
[-Force] [-Name] [-UseTransaction] [-Attributes <FlagsExpression[FileAttributes]> {ReadOnly | Hidden | System |
Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline |
NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}] [-Directory] [-File] [-Hidden] [-ReadOnly]
[-System] [<CommonParameters>]
cmdlets有一些公共的参数:-Verbose, -Debug, -ErrorAction, -ErrorVariable, 和-OutVariable.
可以通过"get-help about_commonparameters“来查看
可以直接使用 Get-ChildItem c:\windows来执行命令,也可以显示指定参数的名称,
如:Get-ChildItem -path c:\windows -filter *.exe -recurse -name
如果参数名称不会造成误解的,甚至可以用:Get-ChildItem -pa c:\windows -fi *.exe -r -n
如果有误解则会得到一个错误:
Get-ChildItem -pa c:\windows -f *.exe -r -n
Get-ChildItem : Parameter cannot be processed because
the parameter name 'f' is ambiguous. Possible matches
include: -Filter -Force.
At line:1 char:14
+ Get-ChildItem <<<< -pa c:\windows -f *.exe -r -n
如果你使用的参数跟某个参数名相同,会得到一个错误。
Write-Host-BackgroundColor
Write-Host : Missing anargument for parameter
'BackgroundColor'. Specifya parameter of type
"System.consoleColor"and try again.
At line:1 char:27
+ Write-Host-BackgroundColor <<<<
这时你可以选择把参数用引号括起来比如
Write-Host "-BackgroundColor"
或者使用 --
Write-Host ---BackgroundColor