Hide powershell output

こ雲淡風輕ζ 提交于 2021-02-17 15:14:35

问题


I have the following script:

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.

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.

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)


回答1:


Option 1: Pipe it to Out-Null

New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo

Option 2: assign to $null (faster than option 1)

$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo

Option 3: cast to [void] (also faster than option 1)

[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo

See also: What's the better (cleaner) way to ignore output in PowerShell?



来源:https://stackoverflow.com/questions/46586382/hide-powershell-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!