- 使用'|'创建管道
- 使用Out-File将输出写入到文本文件.使用-Encoding选项修改编码,使用-append续写(默认覆写).
echo "run 50 us"|.\mips_tb_isim_beh.exe|Out-File isimouttemp.txt -Encoding utf8
- 使用findstr查找文件中的行并输出,默认为正则表达式查找.
findstr "@" isimouttemp.txt|Out-File isimout.txt -Encoding utf8
- 使用rm删除文件
rm .\isimouttemp.txt
- 使用pause暂停
pause
- 使用compare-object或fc.exe比较文件内容
compare-object (get-content .\1.txt) (get-content .\2.txt)|out-file result.txt
fc.exe /a 1.txt 2.txt|out-file result.txt
- 脚本调用其他脚本时,被调脚本中的命令在主调脚本的目录下执行
- 使用tree生成当前目录的子目录树
- 使用ls列出文件,使用-name仅返回文件名
- foreach循环
foreach($a in $list)
{
do something with $a;
}
- 使用echo打印到控制台,使用echo|out-file -append打印到文件
- 脚本参数数组:
$args
, 调用参数:$args[<index>]
, 脚本路径之后(不含脚本路径)的第一个字符串是$args[0]
- Option:
-NAME [VALUE]
,NAME
can be unambigious prefix
pwsh -c "& {<Command>}"
- Option and Value can be refed with unambitious prefix
- Escap: `
|
: 管道- 输出 STDOUT
>
: 覆盖>>
: 追加
- 错误 STDERR
2>
: 覆盖2>>
: 追加
- 全部输出
*>
,*>>
1>log.txt 2>&1
- 指定编码
command 2>&1 | Out-File $mylog -Append -Encoding UTF8
Start-Process -File myjob.bat -RedirectStandardOutput $stdOutLog -RedirectStandardError $stdErrLog -wait
Get-Content $stdErrLog, $stdOutLog | Out-File $myLog -Append -Encoding UTF8
- 关闭:
&-
Job
: List jobsRemove-Job ID|* [-Force]
: Remove job-Force
Get-Help SUBJECT [-Online]
Get-Command NAME
: Locate Command, equivalent of POSIXwhich
Get-Command NAME -All
: Locate all
Get-ChildItem [[-Path] PATH] [OPTION]
- Path: Globable
- Option
-Recurse
-Name
: Name Only-Exclude FILENAME_PATTERN
-Depth NUM
-Force
: Include Hidden-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream | NoScrubData}
: AND Logic-Directory
-File
-Hidden
-ReadOnly
-System
- Mode
l
: Linkd
: Dira
: Archiver
: Read-Onlyh
: Hiddens
: System
Out-File [-FilePath] PATH [[-Encoding] ENCODING] [-Append] [-Force] [-InputObject PSOBJ]
ENCODING
:UTF8
Get-Content|cat [-Path] PATH [-Encoding ENCODING]
Set-Service [-Name] NAME [-DisplayName DISPLAYNAME] [-Description DESCRIPTION] [-StartupType Automatic|AutomaticDelayedStart|Disabled|Manual] [-Status Paused|Running|Stopped]
: Set name, displayname, description, startup type or status of a serviceRestart-Service NAME
Start-Service NAME
Stop-Service NAME
netstat -ano -p tcp | grep -P "PORT"
: Check PID that uses PORT
Get-Process -Id ID
Stop-Process|kill -Id ID
Show-NetFirewallRule | Out-File rule.txt -Encoding UTF8
Set-NetFirewallRule \
[-Name] NAME \
[-NewDisplayName 'DISPLAY_NAME'] \
[-Desription 'DESC'] \
[-Enabled True|False] \
[-Direction Inbound|Outbound] \
[-Action Allow|Block] \
[-Owner OWNER] \
[-Protocol TCP|UDP|Any] \
[-LocalAddress LOCAL_ADDR] \
[-RemoteAddress REMOTE_ADDR] \
[-LocalPort NUM] \
[-RemotePort NUM] \
[-Program "PATH"] \
[-Package PACKAGE] \
[-Service NAME] \
[-LocalUser USER] \
[-RemoteUser USER] \
[-Confrm]
New-NetFirewallRule \
[-DisplayName] 'DISPLAY_NAME' \
[-Name NAME] \
[-Desription 'DESC'] \
[-Enabled True|False] \
[-Direction Inbound|Outbound] \
[-Action Allow|Block] \
[-Owner OWNER] \
[-Protocol TCP|UDP|Any] \
[-LocalAddress LOCAL_ADDR] \
[-RemoteAddress REMOTE_ADDR] \
[-LocalPort NUM] \
[-RemotePort NUM] \
[-Program "PATH"] \
[-Package PACKAGE] \
[-Service NAME] \
[-LocalUser USER] \
[-RemoteUser USER] \
[-Confrm]
New-ItemProperty \
[-Path] "PATH" \
[-Name] NAME \
[-PropertyType String|DWORD] \
[-Value "VALUE"] \
[-Force]
- Path: Registry Database is a Dedicated Drive with following drive mark
HKLM:
forHKEY_LOCAL_MACHINE
New-Item \
-ItemType SymbolicLink \
-Path 'PATH' \
-Target 'TGT_PATH'
findstr "<Regex>" <File>
- PCRE
/P <Path>
- 开始搜索的路径
- 默认为
.
/M <Regex>
- 文件搜索正则
/S
- 递归搜索
/C "<Command>"
- 为每个文件执行的命令
- 可用变量
@file
: 文件名@fname
: 文件主名 (无扩展名)@ext
: 文件扩展名@path
: 文件绝对路径@relpath
: 文件相对路径@isdir
: 是否是目录, 返回"TRUE"或"FALSE"
- Access:
$env:VAR