PowerShell复制到$ null

时间:2011-10-10 19:43:25

标签: powershell

在Windows shell中,您可以使用copy c:\filename NUL之类的命令获取文件的内容并将其复制到“\ Device \ Null”。 (这对于调用外部存档文件非常有用,不会浪费空间或使用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

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

1 个答案:

答案 0 :(得分:4)

实际上,您只想对文件进行读取。如果是这样,这将有效:

Get-ChildItem .\filename | Get-Content | Out-Null

但这可能有点过分了。你可以尝试:

$File=[system.io.file]::OpenRead(".\filename")
$File.Close()

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