#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*" }

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: