| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Returns modules imported by the user session. |
| | 4 | |
|
| | 5 | | .DESCRIPTION |
| | 6 | | Filters the list of currently loaded modules to exclude built-in PowerShell |
| | 7 | | modules, Visual Studio Code host modules and Microsoft.PowerShell.* modules. |
| | 8 | | The result represents modules explicitly imported by the user or scripts. |
| | 9 | | #> |
| | 10 | | function Get-KrUserImportedModule { |
| | 11 | | [CmdletBinding()] |
| | 12 | | [OutputType([System.Management.Automation.PSModuleInfo])] |
| | 13 | | param() |
| | 14 | |
|
| | 15 | | # ----- constants ---------------------------------------------------------- |
| 2 | 16 | | $inboxRoot = [IO.Path]::GetFullPath( (Join-Path $PSHOME 'Modules') ) |
| | 17 | |
|
| | 18 | | # regex fragment that matches “…\.vscode\extensions\ms-vscode.powershell…” |
| 1 | 19 | | $vsCodeRegex = [Regex]::Escape( |
| | 20 | | [IO.Path]::Combine('.vscode', 'extensions', 'ms-vscode.powershell') |
| | 21 | | ) -replace '\\\\', '[\\/]' # make path-separator agnostic |
| | 22 | | # ------------------------------------------------------------------------- |
| | 23 | |
|
| 2 | 24 | | Get-Module | Where-Object { |
| 1 | 25 | | $path = [IO.Path]::GetFullPath($_.ModuleBase) |
| | 26 | |
|
| 1 | 27 | | $isInbox = $path.StartsWith($inboxRoot, |
| | 28 | | $IsWindows ? 'OrdinalIgnoreCase' : 'Ordinal') |
| 1 | 29 | | $isVSCode = $path -match $vsCodeRegex |
| 1 | 30 | | $isMSPSSpace = $_.Name -like 'Microsoft.PowerShell.*' |
| | 31 | |
|
| 2 | 32 | | -not ($isInbox -or $isVSCode -or $isMSPSSpace) |
| | 33 | | } |
| | 34 | | } |
| | 35 | |
|