常见对照
| 批处理写法 | PowerShell 等价 |
|---|---|
| for %%f in (*.log) do ... | Get-ChildItem *.log | ForEach-Object { ... } |
| if exist file | if (Test-Path file) { } |
| findstr /s /i key *.* | Select-String -Path *.* -Pattern key -SimpleMatch |
| set /a n+=1 | $n++ 或 $n += 1 |
| %ERRORLEVEL% | $LASTEXITCODE 与 $? |
常用示例
常用示例
# 统计 D:\Logs 下今天修改过的文件数
(Get-ChildItem D:\Logs -File | Where-Object LastWriteTime -gt (Get-Date).Date).Count迁移注意:$LASTEXITCODE 只反映外部命令退出码;内部命令成败看 $? 与错误流。