Add-PSSnapin
現在のセッションに 1 つまたは複数の Windows PowerShell スナップインを追加します。
構文
Add-PSSnapin
[-Name] <String[]>
[-PassThru]
[<CommonParameters>]
説明
Add-PSSnapin コマンドレットは、登録済みのWindows PowerShell スナップインを現在のセッションに追加します。 スナップインを追加した後、スナップインが現在のセッションでサポートするコマンドレットとプロバイダーを使用できます。
今後のすべてのWindows PowerShell セッションにスナップインを追加するには、add-PSSnapin コマンドを Windows PowerShell プロファイルに追加します。 詳細については、「about_Profiles」を参照してください。
Windows PowerShell 3.0 以降、Windows PowerShell に含まれているコア コマンドはモジュールにパッケージ化されています。 例外は、スナップイン (PSSnapin) の Microsoft.PowerShell.Core です。 既定では、Microsoft.PowerShell.Core スナップインのみがセッションに追加されます。 モジュールは最初の使用時に自動的にインポートされ、Import-Module コマンドレットを使用してインポートできます。
例
例 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 コマンドレットと 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
スナップインの名前を指定します。 これは名前であり、AssemblyName または ModuleName ではありません。 ワイルドカードを使用できます。
システムに登録されているスナップインの名前を検索するには、「 」と入力します Get-PSSnapin -Registered
。
Type: | String[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-PassThru
このコマンドレットは、追加された各スナップインを表す オブジェクトを返します。 既定では、このコマンドレットによる出力はありません。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
入力
None
このコマンドレットにパイプを使用してオブジェクトを渡すことはできません。
出力
None or System.Management.Automation.PSSnapInInfo
このコマンドレットは、 PassThru パラメーターを指定した場合にスナップインを表す PSSnapInInfo オブジェクトを返します。 それ以外の場合、このコマンドレットによる出力はありません。
メモ
Windows PowerShell 3.0 以降、Windows PowerShell と共にインストールされるコア コマンドはモジュールにパッケージ化されています。 Windows PowerShell 2.0 およびそれ以降のバージョンのWindows PowerShellで古いスタイルのセッションを作成するホスト プログラムでは、コア コマンドはスナップイン (PSSnapins) にパッケージ化されています。 例外は Microsoft.PowerShell.Core です。これは常にスナップインです。 また、New-PSSession コマンドレットによって開始されるリモート セッションは、コア スナップインを含む古いスタイルのセッションです。
コア モジュールを使用して新しいスタイルのセッションを作成する CreateDefault2 メソッドの詳細については、MSDN ライブラリの CreateDefault2 メソッド に関するページを参照してください。
スナップインの詳細については、「about_PSSnapins」および「MSDN ライブラリのWindows PowerShell スナップインをCreateする方法」を参照してください。
Add-PSSnapin は、スナップインを現在のセッションにのみ追加します。 スナップインをすべての Windows PowerShell セッションに追加するには、そのスナップインを Windows PowerShell プロファイルに追加します。 詳細については、「about_Profiles」を参照してください。
Microsoft .NET Framework インストール ユーティリティを使用して、登録されている任意のスナップインを追加できます。 詳細については、「MSDN ライブラリ でコマンドレット、プロバイダー、ホスト アプリケーションを登録する方法 」を参照してください。
コンピューターに登録されているスナップインの一覧を取得するには、「 」と入力します
Get-PSSnapin -Registered
。アドインを追加する前に、Add-PSSnapin はスナップインのバージョンを確認して、現在のバージョンのWindows PowerShellと互換性があることを確認します。 スナップインがバージョン チェックに失敗した場合、Windows PowerShell はエラーを報告します。