隐藏 powershell 输出

J. *_*Doe 34 powershell

我有以下脚本:

param([Parameter(Mandatory=$true)][string]$dest)

New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory

Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force
Run Code Online (Sandbox Code Playgroud)

该脚本采用一个字符串并将所有文件和文件夹从静态原始路径复制到给定的根字符串,为了结构清晰而修改一些文件夹。

它工作正常,但打印出“New-Item”命令的结果,我想隐藏它。我查看了有关 SE 的网络和其他问题,但没有找到我的问题的明确答案。

如果有人想知道 - 我在开头使用“New-item”是为了规避 PS' -recurse 参数中的缺陷,如果目标文件夹不存在,则不会正确复制所有子文件夹。(即它们是强制性的)

Chi*_*ago 48

选项 1:将其通过管道传输到 Out-Null

New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
Run Code Online (Sandbox Code Playgroud)

选项 2:分配给$null(比选项 1 快)

$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
Run Code Online (Sandbox Code Playgroud)

选项 3:转换为[void](也比选项 1 快)

[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
Run Code Online (Sandbox Code Playgroud)

另请参阅:在 PowerShell 中忽略输出的更好(更干净)方法是什么?