隐藏 powershell 输出 [英] Hide powershell output
问题描述
我有以下脚本:
param([Parameter(Mandatory=$true)][string]$dest)
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
脚本采用一个字符串并将所有文件和文件夹从静态源路径复制到给定的根字符串,为了结构清晰而修改了一些文件夹.
The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.
它工作正常,但打印出New-Item"命令的结果,我想隐藏它.我查看了有关 SE 的网络和其他问题,但没有找到我的问题的明确答案.
It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.
如果有人想知道 - 我在开头使用New-item"是为了规避 PS' -recurse 参数中的缺陷,如果目标文件夹不存在,则不会正确复制所有子文件夹.(即它们是强制性的)
In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)
推荐答案
Option 1: Pipe it to Out-Null
Option 1: Pipe it to Out-Null
New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
选项 2:分配给 $null
(比选项 1 快)
Option 2: assign to $null
(faster than option 1)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
选项 3:转换为 [void]
(也比选项 1 快)
Option 3: cast to [void]
(also faster than option 1)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
另见:什么是更好(更清洁)在 PowerShell 中忽略输出的方法?
这篇关于隐藏 powershell 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!