The PowerShell gallery has a script module (credit: Steve Lee [MSFT]) you can install that appends your Windows PSModulePath contents to the PSModulePath in PowerShell Core. This lets you access modules in any of those Windows PSModulePaths within PowerShell Core, including your custom modules.
(Note: I’ve already commented on the gallery of what this post will cover. I’ve added it here in case one does not come across the PS Gallery.)
This script works but it adds all paths existing in Windows. This causes duplicate paths to exist in PSModulePath in PowerShell Core as some paths already exist in it as well.
Here is that script revised so that it adds only paths that don’t exist in PowerShell Core $env:PSModulePath:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Function Add-WindowsPSModulePath {
if (! $IsWindows) {
throw "This cmdlet is only supported on Windows"
}
$WindowsPSModulePath = [System.Environment]::GetEnvironmentVariable("psmodulepath",
[System.EnvironmentVariableTarget]::Machine)
$WinPaths = ($WindowsPSModulePaths -split ';')
foreach ($path in $WinPaths) {
if (-not ($env:PSModulePath).Contains($path)) {
$env:PSModulePath += ";$path}"
}
}
}
You can copy this code into your profile.ps1 file so it will execute each time you open PowerShell Core:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Function Add-WindowsPSModulePath {
if (! $IsWindows) {
throw "This cmdlet is only supported on Windows"
}
$WindowsPSModulePath = [System.Environment]::GetEnvironmentVariable("psmodulepath",
[System.EnvironmentVariableTarget]::Machine)
$WinPaths = ($WindowsPSModulePaths -split ';')
foreach ($path in $WinPaths) {
if (-not ($env:PSModulePath).Contains($path)) {
$env:PSModulePath += ";$path}"
}
}
}
Add-WindowsPSModulePath
Comments powered by Disqus.