powershell - 使用 Where-Object 打开文件
问题描述
我搜索一个文件,例如 hosts 文件:
cd c:\Windows\System32
gci -Recurse | ? {$_.Name -eq 'hosts'}
现在我想在 中打开文件notepad
,所以我尝试了:
gci -Recurse | ? {$_.Name -eq 'hosts'} | notepad.exe $_.FullName
这个错误。有没有办法做到这一点,作为一个单线?
解决方案
notepad.exe
不接受管道输出输入
Get-ChildItem -Recurse -ErrorAction SilentlyContinue |
Where-Object -FilterScript { $_.Name -eq 'hosts' } |
Foreach-Object -Process { notepad.exe $_.FullName }
为此,我建议在 get-childitem 上使用 -Filter。它将大大提高脚本的性能。-@马特
Get-ChildItem -Filter Hosts -Recurse -ErrorAction SilentlyContinue |
ForEach-Object -Process { notepad.exe $_.FullName }
推荐阅读
- python - 热图图例中的重叠标签-如何在 R 中的 ggplot 中移动图例标签?
- php - WordPress 类别页面上的 CSS 网格布局
- swift - 【核心数据】:数据:
- nginx - 带有 socks5 代理的 nginx proxy_pass?
- php - 如何在 PHP 中使用循环动态创建 html 表?
- python - 用另一个类方法装饰类方法
- swift - 如何在核心数据一对多关系的上下文中从表视图中删除行?(迅速)
- python - 为什么 os.path.join 返回带有 2 个斜杠的结果?
- java - lambda 表达式中的错误返回类型:Model1 无法转换为 Model2
- node.js - nodejs fluent-ffmpeg 进程太慢