Add-PSSnapin

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

语法

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 管理单元添加到会话中。 它使用带有 Registered 参数的 Get-PSSnapin cmdlet 来获取表示每个已注册管理单元的对象。管道运算符 (|) 将结果传递给 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

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-PassThru

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

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

None

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

输出

None or System.Management.Automation.PSSnapInInfo

如果指定 PassThru 参数,此 cmdlet 将返回表示管理单元的 PSSnapInInfo 对象。 否则,此 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如何Create Windows PowerShell管理单元

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

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

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

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