2021-07-21 初稿

ps命令采用动词-名词的方式命名,不区分大小写。默认当前文件夹为当前路径​​./​​。除去​​-match​​使用正则表达式匹配外,其他都使用​​*​​和​​?​​通配符。

速查

管道命令

使$_ | 
get-childitem "./" | foreach-object -process{
write-host $_
}


​重定向命令​

>  #覆盖重写 dir > dir.txt 等价于dir | out-file dir.txt 
>> #追加到原文本
2> #将错误输出
*> #将所有信息输出


遍历

foreach($a in dir "./caizihua*") {<code block>}

get-childitem -path "./*" [-filter] [-include] | where{$_ -match "<condition>"} | foreach-object [-begin{}] -process{}


​字符串运算​

string.split("\.,#") #string.split(".") string.split("[,.#]")
-replace -creplace()
-eq -ceq()
-like() -clike
-match(m) -cmatch
-notmatch -cnotmatch
insert(pos,sting)
substring(A[,B]) #从A开始,一共B个(没有B,默认A之后所有) $_.name.substring(0,$_.length-4)


其他

# ps1脚本中调用powershell
powershell.exe -file <name>
# 复制
copy-item <origin> -destination <des>
# 移除
remove-item <file directory>
# 创建
new-item -path <path> [-tpye <file|directory>] [-name <string>] # ni "./../caizihua.txt"(在上一层目录创建)
#($_表示文件)
$_.name $_.basename


等待输入

$a=read-host "reminder" # reminder:


​条件比较​

-eq -ne -gt -ge -lt -le -contains -notcontains
-not -and -or -xor()


​格式化输出​

# 数字表示精度
"{0:N2}" -f $a #小数
348.00
"{0:D8}" -f $a #整数补齐
00000348
"{0:C2}" -f $a #当地货币
$348.00
"{0:P0}" -f $a #百分数
34,800 %
"{0:X0}" -f $a #十六进制
15C


​转义​

双引号​​""​​变量代换字符转义,单引号​​''​​按字面解释。抑音符​​`​​是powershell特有的转义字符。

#输出字符串"a",但是如下会报错(因为前两个双引号会结合在一起,而a没有被任何双引号括起来)
$a=""a"" #错误
#正确写法如下,使用单引号‘按字面解释’,或者使用抑音符`转义所要字符"
$a='"a"'
$a="`"a`""


关于在双引号中括有双引号,单引号中扩有单引号,string中使用​​""​​表示​​"​​(​​''​​表示​​'​​)

#输出字符串"a"
$a="""a""" # 等价于$a="`"a`""
#输出字符串'a'
$a='''a''' # 等价于$a="`'a`'"。而使用$a='`'a'`'会报错,因为单引号是按字面意思解释,抑音符没用,a没有被任何单引号括起来


输出变量​​"i'm caizihua"​​。

$a=" `"i`'m caizihua`" "
$a=' "i''m caizihua" '
$a=
@"
"i'm caizihua
"@ #关键词here-strings,其中引号按原意解释


​get-childitem​

快捷名​​dir​​、​​ls​​、​​gci​​,默认访问当前目录​​./​​。

Get-ChildItem
[[-Path] <string[]>] #指定路径,可代替部分filter和include的功能
[[-Filter] <string>] #筛选出与string相符合的文件或文件夹
[-Include <string[]>]
[-Exclude <string[]>]
[-Recurse] #递归遍历
[-Depth <uint32>] #递归遍历深度
[-Force]
[-Name] #只显示文件或文件夹名
[-Attributes <FlagsExpression[FileAttributes]>]
[-FollowSymlink]
[-Directory] #遍历文件夹
[-File] #遍历文件
[-Hidden]
[-ReadOnly]
[-System]
[<CommonParameters>]


例子:遍历当前文件夹下的​​jpg​​文件。

get-childitem -path "./*.jpg"
get-childitem -path "./" -filter "*.jpg" #filter只能过滤一种类型。


使用​​-include​​,可以筛选多种信息,使用 ​​,​​ 隔开。使用​​-include​​时,必须将​​-path​​指定为目录下的文件,如​​./*​​,或者使用​​-recurse​​。

get-childitem -path "./*" -include "*.jpg","*.xml"    #当前文件夹中所有的*.jpg和*.xml文件,


​rename​

快捷名​​ren​

Rename-Item
-LiteralPath <String> #按字面解释
[-Path] <String>
[-NewName] <String>
[-Force]
[-PassThru]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]d


Powershell中有一些保留字符,如​​[ ] ` ' " * ? ​​ 。文件命中允许存在方括号​​[]​​,使用普通的命名方式会报错。

自己输入文件名的方式,会报错。(单引号按字面解释,所以是bug?)

powershell命令总结_Windows

如下为使用tab缩进自动补全文件名的方式重命名,也会出错

powershell命令总结_双引号_02

参数​​-literalpath <path>​​ 不会转义​​<path>​​中的内容,所以如下会成功

rename-item -literalpath "./cai[zi]hua.txt" -newname "boogie.txt"
#文件名含有方括号[]时,可以加上这个参数
get-child-item "./*.jpg" | foreach-pbject -begin{$index=0} -process{
$n="caizihua.{0:D2}.jpg" -f $index; # -f 是格式化输出
$index++;
rename-item -literalpath $_ -newname $n
}


例子

​powershell​​字符串的处理

现在有很多​​caizihua.X.jpg​​,其中​​XX​​为格式整数​​1~99​​,现在将其变成​​00~99​

dir "./*" | where-object{$_ -match ".*\.jpg$"} | foreach-item -process{
$n=$_.name.split("\.");
$num=[convert]::toint32($n[1],10); #10表示十进制
$fn="caizihua.{0:D2}.jpg" -f $num
rename-item $_ -newname $fn
}