#PSTip Getting system modules only

When we use the Get-Module -ListAvailable command to list installed modules, we get a list of modules from two default module locations: one for the system and one for the current user.

By default, the current user modules are located under the current user’s Documents (MyDocuments) folder and system modules reside under the PowerShell installation directory. To view the default module locations, type:

PS> $env:PSModulePath

The value is hard to read and is displayed in one long line delimited by a semi colon. To display the paths, one on its own line, type:

PS> $env:PSModulePath -split ';'

Each module info object returned by Get-Module has a Path property. We can use the Path to programmatically determine if a module is a user module or a system module. For system modules, we can check if the module path starts with the path of Windows PowerShell installation directory:

PS> Get-Module -ListAvailable | Where-Object {$_.Path -like "$PSHOME*" }

To produce a list of user modules, we can simply negate the filter using the -NotLike operator:

PS>  Get-Module -ListAvailable | Where-Object {$_.Path -notlike "$PSHOME*" }
Share on:
comments powered by Disqus