如何防止Powershell不显示脚本处理数据

How to prevent Powershell not showing script processing data

我已经创建了一个用于更新 SharePoint 列表的 powershell 脚本。当我 运行 它向控制台显示所有后端处理,例如所有列表架构信息。我希望它只显示创建的脚本 Out put(Write-Host 的唯一输出)而不是开箱即用的后端处理。有什么命令可以防止这种情况发生吗?

当我在脚本中的任何地方使用 ArrayList 的 .add 方法时,它显示如下计数

0
1
2
3

当我将列表项存储在脚本中的某个变量中时,它显示列表 XML 架构,如下所示:

Sealed                      : False
Version                     : 28
DisplayFormTemplateName     : DocumentLibraryForm
EditFormTemplateName        : DocumentLibraryForm
NewFormTemplateName         : DocumentLibraryForm
NewFormUrl                  : 
MobileNewFormUrl            : 
EditFormUrl                 : 
MobileEditFormUrl           : 
DisplayFormUrl              : 
MobileDisplayFormUrl        : 
Id                          : 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39009893A4DD0A05487AAE05EAE8D183333C003933922476541344B8A065CBACE8178D
ReadOnly                    : False
Name                        : NewsPage
NameResource                : Microsoft.SharePoint.SPUserResource
FeatureId                   : 00000000-0000-0000-0000-000000000000
Description                 : Used to create news articles.
JSLink                      : 
DescriptionResource         : Microsoft.SharePoint.SPUserResource
Hidden                      : False

一些 .Net 方法和外部程序将它们的退出代码和其他输出输出到标准输出。

您可以将 void 关键字添加到 arraylist "Add" 命令或将其通过管道传递给 Out-Null

$MyList = [System.Collections.ArrayList]@()
1..10 | % { $MyList.Add($_) } # outputs 0 to 9

1..10 | % { [void]$MyList.Add($_) } # no output
1..10 | % { $MyList.Add($_) | Out-Null } # no output