如何在PowerShell中关闭所有打开的网络文件?

Wid*_*dmo 6 powershell batch-file

我正在尝试将我的旧BAT脚本转换为PowerShell版本,但在一小时谷歌搜索后我不知道该怎么做.

我正在寻找一个与旧的结构非常相似的结构,找到开放的网络文件,得到它的PID并关闭它.

蝙蝠:

for /f "skip=4 tokens=1" %a in ('net files ^| findstr C:\Apps\') do net files %a /close
Run Code Online (Sandbox Code Playgroud)

电源外壳?

Jay*_*uzi 7

这是另一种方式.我喜欢它更依赖于流水线操作,这是PowerShell的成语:

net files | 
    where   { $_.Contains( "D:\" ) } |
    foreach { $_.Split( ' ' )[0] }   |
    foreach { net file $_ /close }
Run Code Online (Sandbox Code Playgroud)


Fro*_* F. 5

网络文件仍然是您最好的选择。尝试这样的事情:

$results = net file | Select-String -SimpleMatch "C:\Apps\"
foreach ($result in $results) {
    #Get id
    $id = $result.Line.Split(" ")[0]

    #Close file
    net file $id /close

}
Run Code Online (Sandbox Code Playgroud)