开发者

PowerShell copy to $null

开发者 https://www.devze.com 2023-04-12 14:43 出处:网络
In Windows shell you can fetch the contents of a file and copy it to \"\\Device\\Null\" with a command like copy c:\\filename NUL. (This is useful for recalling externally-archived files without wasti

In Windows shell you can fetch the contents of a file and copy it to "\Device\Null" with a command like copy c:\filename NUL. (This is useful for recalling externally-archived files without wasting space or updating with touch.)

But I can't figure out how to do the same in PowerShell using $null, NUL, \\.\NUL and more (and I don't want to call out a separate CMD.EXE process to do this for every file).

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          : Writ开发者_运维问答eError: (\\.\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Any other ideas how to do this?


Effectively, you just want to do a read on the file. If so, this will work:

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

It's probably overkill though. You could try:

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

This just opens the file for reading (which may be enough to bring it back) and closes it again.

0

精彩评论

暂无评论...
验证码 换一张
取 消