Get-Module
获取已导入或可以导入到当前会话中的模块。
语法
Get-Module [-全部] [-ListAvailable] [-Name <string[]>] [<CommonParameters>]
Get-Module [[-Name] <string[]>] [<CommonParameters>]
说明
Get-Module cmdlet 获取已导入或可以导入到会话中的模块。
Get-Module 只获取模块;但不导入模块。要将模块导入到会话中,请使用 Import-Module。
参数
-全部
获取所有模块文件的模块对象。
如果不使用 All 参数,则 Get-Module 只获取默认模块文件的模块对象。该 cmdlet 选择文件类型的顺序如下:清单 (.psd1) 文件、脚本模块 (.psm1) 文件和二进制模块 (.dll) 文件。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-ListAvailable
获取可以导入到会话中的所有模块。Get-Module 获取由 $env:PSModulePath 环境变量指定的路径中的模块。
如果不使用此参数,Get-Module 只获取已导入到会话中的模块。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Name <string[]>
只获取具有指定名称或名称模式的模块。允许使用通配符。还可以通过管道将名称传送给 Get-Module。
是否为必需? |
false |
位置? |
1 |
默认值 |
所有的或可用的模型。 |
是否接受管道输入? |
true (ByValue) |
是否接受通配符? |
true |
<CommonParameters>
此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.
输入和输出
输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。
输入 |
System.String 可以通过管道将模块名称传递给 Get-Module。 |
输出 |
System.Management.Automation.PSModuleInfo Get-Module 返回表示模块的对象。 |
说明
还可以通过别名“gmo”来引用 Get-Module。有关详细信息,请参阅 about_Aliases。
All 参数返回文件扩展名为 .dll 的所有文件的模块对象,即使它们不实现 cmdlet 或提供程序也是如此。
示例 1
C:\PS>get-module
说明
-----------
此命令获取已导入到当前会话中的模块。
示例 2
C:\PS>get-module -listAvailable
说明
-----------
此命令获取可以导入到当前会话中的模块。
Get-Module 在由 $env:PSModulePath 环境变量指定的路径中查找可用模块。有关 PSModulePath 的详细信息,请参阅 about_Modules 和 about_Environment_Variables。
示例 3
C:\PS>get-module -listAvailable -all
说明
-----------
此命令获取所有可用模块的全部导出文件。
示例 4
C:\PS>get-module | get-member -type property
TypeName: System.Management.Automation.PSModuleInfo
Name MemberType Definition
---- ---------- ----------
AccessMode Property System.Management.Automation.ModuleAcc
Description Property System.String Description {get;set;}
ExportedAliases Property System.Collections.Generic.Dictionary`
ExportedCmdlets Property System.Collections.Generic.Dictionary`
ExportedFunctions Property System.Collections.Generic.Dictionary`
ExportedVariables Property System.Collections.Generic.Dictionary`
Guid Property System.Guid Guid {get;}
ModuleBase Property System.String ModuleBase {get;}
ModuleType Property System.Management.Automation.ModuleTyp
Name Property System.String Name {get;}
NestedModules Property System.Collections.ObjectModel.ReadOnl
OnRemove Property System.Management.Automation.ScriptBlo
Path Property System.String Path {get;}
PrivateData Property System.Object PrivateData {get;set;}
SessionState Property System.Management.Automation.SessionSt
Version Property System.Version Version {get;}
说明
-----------
此命令获取 Get-Module 返回的 PSModuleInfo 对象的属性。每个模块文件对应一个对象。
可以使用属性来设置模块对象的格式以及筛选模块对象。有关这些属性的详细信息,请参阅 MSDN (Microsoft Developer Network) Library 中的“PSModule 属性”,网址是 https://go.microsoft.com/fwlink/?LinkId=143624。
示例 5
C:\PS>get-module -listAvailable -all | format-table -property name, moduletype, path -groupby name -auto
Name: MyTestCmdlets
Name ModuleType Path
---- ---------- ----
MyTestCmdlets Script C:\Windows\system32\WindowsPowerShell\v1.0\Modules\TestCmdlets\TestCmdlets.dll
Name: PSDiagnostics
Name ModuleType Path
---- ---------- ----
PSDiagnostics Manifest C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psd1
PSDiagnostics Script C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psm1
Name: FileTransfer
Name ModuleType Path
---- ---------- ----
FileTransfer Manifest C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer\FileTransfer.psd1
说明
-----------
此命令获取所有的模块文件(已导入的和可用的)并按模块名称对其进行分组。这样一来,就可以看到每个脚本正导出的模块文件。
示例 6
C:\PS>$m = get-module -list -name FileTransfer | where {$_.moduletype -eq "Manifest"}
C:\PS> get-content $m.path
@{
GUID="{8FA5064B-8479-4c5c-86EA-0D311FE48875}"
Author="Microsoft Corporation"
CompanyName="Microsoft Corporation"
Copyright="© Microsoft Corporation. All rights reserved."
ModuleVersion="1.0.0.0"
Description="Windows Powershell File Transfer Module"
PowerShellVersion="2.0"
CLRVersion="2.0"
NestedModules="Microsoft.BackgroundIntelligentTransfer.Management"
FormatsToProcess="FileTransfer.Format.ps1xml"
RequiredAssemblies=Join-Path $psScriptRoot "Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll"
}
说明
-----------
这些命令显示了 Windows PowerShell 文件传输模块的模块清单的内容。
第一条命令获取表示文件传输模块的模块清单的 PSModuleInfo 对象它将对象保存在 $m 变量中。
第二条命令使用点表示法获取清单文件的路径,后者存储在对象的 Path 属性中。然后,它使用 Get-Content cmdlet 获取指定路径下的清单文件的内容。
模块不一定要有清单文件。如果模块有清单文件,则清单中只要求包含版本号。不过,清单文件通常会提供有关模块、模块要求以及模块内容的有用信息。
示例 7
C:\PS>get-module -listAvailable -name FileTransfer | format-list -property *
Name : FileTransfer
Path : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer\FileTransfer.psd1
Description : Powershell File Transfer Module
Guid : 8fa5064b-8479-4c5c-86ea-0d311fe48875
ModuleBase : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer
PrivateData :
Version : 1.0.0.0
ModuleType : Manifest
AccessMode : ReadWrite
ExportedFunctions : {}
ExportedCmdlets : {}
NestedModules : {}
ExportedVariables : {}
ExportedAliases : {}
SessionState : System.Management.Automation.SessionState
OnRemove :
说明
-----------
此命令在列表中显示 FileTransfer 模块的所有属性。
因为该模块尚未导入到会话中,所以尚未填充 Exported* 属性和 NestedModules 属性。只有在元素已导出并且嵌套模块已实例化后,才会填充这些属性。
示例 8
C:\PS>dir (get-module -listavailable FileTransfer).modulebase
Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 12/16/2008 12:36 PM en-US
-a--- 11/19/2008 11:30 PM 16184 FileTransfer.Format.ps1xml
-a--- 11/20/2008 11:30 PM 1044 FileTransfer.psd1
-a--- 12/16/2008 12:20 AM 108544 Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll
说明
-----------
此命令列出模块的目录中的文件。这是导入模块前确定模块中内容的另一种方式。有些模块可能有帮助文件或描述模块的自述文件。