Add-PSSnapin

将一个或多个 Windows PowerShell 管理单元添加到当前会话。

语法

Default (默认值)

Add-PSSnapin
    [-Name] <String[]>
    [-PassThru]
    [<CommonParameters>]

说明

Add-PSSnapin cmdlet 将已注册的 Windows PowerShell 管理单元添加到当前会话。 添加管理单元后,可以使用管理单元在当前会话中支持的 cmdlet 和提供程序。

若要将管理单元添加到所有未来的 Windows PowerShell 会话,请将 Add-PSSnapin 命令添加到 Windows PowerShell 配置文件。 有关详细信息,请参阅about_Profiles。

从 Windows PowerShell 3.0 开始,Windows PowerShell 中包含的核心命令打包在模块中。 例外情况是 Microsoft.PowerShell.Core,这是管理单元(PSSnapin)。 默认情况下,只会将 Microsoft.PowerShell.Core 管理单元添加到会话。 模块在首次使用时自动导入,可以使用 Import-Module cmdlet 导入它们。

示例

示例 1:添加管理单元

PS C:\> Add-PSSnapIn -Name Microsoft.Exchange, Microsoft.Windows.AD

此命令将 Microsoft Exchange 和 Active Directory 管理单元添加到当前会话。

示例 2:添加所有已注册管理单元

PS C:\> Get-PSSnapin -Registered | Add-PSSnapin -Passthru

此命令将所有已注册的 Windows PowerShell 管理单元添加到会话。 它使用 Get-PSSnapin cmdlet 和 Registered 参数来获取表示每个已注册管理单元的对象。管道运算符 (|) 将结果传递给 Add-PSSnapin,这会将它们添加到会话中。 passThru 参数 返回表示添加的每个管理单元的对象。

示例 3:注册管理单元并添加它

The first command gets snap-ins that have been added to the current session that include the snap-ins that are installed with Windows PowerShell. In this example, ManagementFeatures is not returned. This indicates that it has not been added to the session.
PS C:\> Get-PSSnapin

The second command gets snap-ins that have been registered on your system, which includes those that have already been added to the session. It does not include the snap-ins that are installed with Windows PowerShell. In this case, the command does not return any snap-ins. This indicates that the ManagementFeatures snapin has not been registered on the system.
PS C:\> Get-PSSnapin -Registered

The third command creates an alias, installutil, for the path of the InstallUtil tool in .NET Framework.
PS C:\> Set-Alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil.exe

The fourth command uses the InstallUtil tool to register the snap-in. The command specifies the path of ManagementCmdlets.dll, the file name or module name of the snap-in.
PS C:\> installutil C:\Dev\Management\ManagementCmdlets.dll

The fifth command is the same as the second command. This time, you use it to verify that the ManagementCmdlets snap-in is registered.
PS C:\> Get-PSSnapin -Registered

The sixth command uses the **Add-PSSnapin** cmdlet to add the ManagementFeatures snap-in to the session. It specifies the name of the snap-in, ManagementFeatures, not the file name.
PS C:\> add-pssnapin ManagementFeatures

To verify that the snap-in is added to the session, the seventh command uses the *Module* parameter of the Get-Command cmdlet. It displays the items that were added to the session by a snap-in or module.
PS C:\> Get-Command -Module ManagementFeatures

You can also use the **PSSnapin** property of the object that the **Get-Command** cmdlet returns to find the snap-in or module in which a cmdlet originated. The eighth command uses dot notation to find the value of the PSSnapin property of the Set-Alias cmdlet.
PS C:\> (Get-Command Set-Alias).pssnapin

此示例演示了在系统上注册管理单元,然后将其添加到会话的过程。 它使用 ManagementFeatures,这是在名为 ManagementCmdlets.dll的文件中实现的虚构管理单元。

参数

-Name

指定管理单元的名称。 这是 Name,而不是 AssemblyName 或 ModuleName。 允许使用通配符。

若要在系统上查找已注册管理单元的名称,请键入 Get-PSSnapin -Registered

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-PassThru

指示此 cmdlet 返回一个对象,该对象表示每个添加的管理单元。 默认情况下,此 cmdlet 不生成任何输出。

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

此 cmdlet 支持通用参数:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 有关详细信息,请参阅 about_CommonParameters

输入

None

不能通过管道将对象传递给此 cmdlet。

输出

None or System.Management.Automation.PSSnapInInfo

如果 指定了 passThru 参数,此 cmdlet 将返回一个 PSSnapInInInfo 对象,该对象代表管理单元。 否则,此 cmdlet 不会生成任何输出。

备注

  • 从 Windows PowerShell 3.0 开始,随 Windows PowerShell 一起安装的核心命令打包在模块中。 在 Windows PowerShell 2.0 和在更高版本的 Windows PowerShell 中创建旧式会话的主机程序中,核心命令打包在管理单元(PSSnapins)中。 例外情况是 Microsoft.PowerShell.Core,该管理单元始终是管理单元。 此外,远程会话(例如由 New-PSSession cmdlet 启动的会话)是包含核心管理单元的旧式会话。

    有关使用核心模块创建新样式会话的 CreateDefault2 方法的信息,请参阅 MSDN 库中 CreateDefault2 方法

  • 有关管理单元的详细信息,请参阅 MSDN 库中 about_PSSnapins如何创建 Windows PowerShell 管理单元

  • Add-PSSnapin 仅将管理单元添加到当前会话。 若要将管理单元添加到所有 Windows PowerShell 会话,请将其添加到 Windows PowerShell 配置文件。 有关详细信息,请参阅about_Profiles。

  • 可以使用 Microsoft .NET Framework 安装实用工具添加已注册的任何管理单元。 有关详细信息,请参阅 MSDN 库中 如何注册 Cmdlet、提供程序和主机应用程序

  • 若要获取计算机上注册的管理单元列表,请键入 Get-PSSnapin -Registered

  • 在添加管理单元之前,Add-PSSnapin 检查管理单元的版本,以验证它是否与当前版本的 Windows PowerShell 兼容。 如果管理单元未通过版本检查,Windows PowerShell 将报告错误。