PowerShell复制到$ null

ewa*_*all 3 powershell

在Windows Shell中,您可以使用像这样的命令来获取文件的内容并将其复制到“ \ Device \ Null” copy c:\filename NUL。(这对于在不浪费空间或使用进行更新的情况下调用外部归档文件很有用touch。)

但我无法弄清楚如何使用做同样在PowerShell中$nullNUL\\.\NUL和更多的(我不想调出一个单独的CMD.EXE的过程,为每个文件做到这一点)。

PS C:\> Copy-Item -Path  .\filename -Destination NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  -Path  .\filename -Destination NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> Copy-Item .\filename NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  .\filename NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> copy .\filename '\\.\NUL'
Copy-Item : Cannot process path '\\.\NUL' because the the target represents a reserved device name.
At line:1 char:5
+ copy <<<<  .\filename '\\.\NUL'
    + CategoryInfo          : WriteError: (\\.\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
Run Code Online (Sandbox Code Playgroud)

还有其他想法如何做到这一点?

小智 5

实际上,您只想对文件进行读取。如果是这样,它将起作用:

Get-ChildItem .\filename | Get-Content | Out-Null
Run Code Online (Sandbox Code Playgroud)

不过,这可能太过分了。您可以尝试:

$File=[system.io.file]::OpenRead(".\filename")
$File.Close()
Run Code Online (Sandbox Code Playgroud)

这只是打开文件进行读取(可能足以将其带回)并再次关闭它。