Skip to content

Commit 536b1ed

Browse files
committed
Use pwsh variable PSContentPath instead of cmdlet Get-PSContentPath
1 parent 26c5bf8 commit 536b1ed

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

src/code/Utils.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,37 +1160,34 @@ private static string GetHomeOrCreateTempHome()
11601160
private readonly static Version PSVersion7_7 = new Version(7, 7);
11611161

11621162
/// <summary>
1163-
/// Gets the user content directory path using PowerShell's Get-PSContentPath cmdlet.
1164-
/// Falls back to legacy path if the cmdlet is not available or PowerShell version is below 7.7.0.
1163+
/// Gets the user content directory path using PowerShell's $PSUserContentPath variable.
1164+
/// Falls back to legacy path if the variable is not available or PowerShell version is below 7.7.0.
11651165
/// </summary>
11661166
private static string GetUserContentPath(PSCmdlet psCmdlet, Version psVersion, string legacyPath)
11671167
{
11681168

1169-
// Only use Get-PSContentPath cmdlet if PowerShell version is 7.7.0 or greater (when PSContentPath feature is available)
1169+
// Only use PSContentPath features if PowerShell version is 7.7.0 or greater (when PSContentPath feature is available)
11701170
if (psVersion >= PSVersion7_7)
11711171
{
1172-
// Try to use PowerShell's Get-PSContentPath cmdlet in the current runspace
1173-
// This cmdlet is only available if experimental feature PSContentPath is enabled
1172+
// Try to get the readonly $PSUserContentPath variable (PowerShell 7.7+ with PSContentPath enabled)
11741173
try
11751174
{
1176-
var results = psCmdlet.InvokeCommand.InvokeScript("Get-PSContentPath");
1177-
1178-
if (results != null && results.Count > 0)
1175+
var contentPathVar = psCmdlet.SessionState.PSVariable.GetValue("PSUserContentPath");
1176+
if (contentPathVar != null)
11791177
{
1180-
// Get-PSContentPath returns a PSObject, extract the path string
1181-
string userContentPath = results[0]?.ToString();
1178+
string userContentPath = contentPathVar.ToString();
11821179
if (!string.IsNullOrEmpty(userContentPath))
11831180
{
1184-
psCmdlet.WriteVerbose($"User content path from Get-PSContentPath: {userContentPath}");
1185-
InternalHooks.LastUserContentPathSource = "Get-PSContentPath";
1181+
psCmdlet.WriteVerbose($"User content path from $PSUserContentPath variable: {userContentPath}");
1182+
InternalHooks.LastUserContentPathSource = "$PSUserContentPath";
11861183
InternalHooks.LastUserContentPath = userContentPath;
11871184
return userContentPath;
11881185
}
11891186
}
11901187
}
11911188
catch (Exception ex)
11921189
{
1193-
psCmdlet.WriteVerbose($"Get-PSContentPath cmdlet not available: {ex.Message}");
1190+
psCmdlet.WriteVerbose($"$PSUserContentPath variable not available: {ex.Message}");
11941191
}
11951192
}
11961193
else

0 commit comments

Comments
 (0)