Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

studio setup: let Windows PowerShell load its own Security module (#7692)

Daniel Han479b46e8a95d
diff --git a/.github/workflows/studio-windows-update-smoke.yml b/.github/workflows/studio-windows-update-smoke.yml
index 80cc5a1f9ba..12283517728 100644
--- a/.github/workflows/studio-windows-update-smoke.yml
+++ b/.github/workflows/studio-windows-update-smoke.yml
@@ -25,6 +25,7 @@ on:
- 'install.ps1'
- 'scripts/uninstall.ps1'
- 'tests/studio/test_uninstall_*.ps1'
+ - 'tests/studio/test_psmodulepath_normalization.ps1'
- 'studio/setup.ps1'
- 'studio/setup.bat'
- 'studio/install_python_stack.py'
@@ -73,6 +74,12 @@ jobs:
pwsh -NoProfile -File tests/studio/test_uninstall_dual_install_icon.ps1
if ($LASTEXITCODE) { exit $LASTEXITCODE }
+ # Both entry points prepend the 5.1 system module dir, and Refresh-Environment
+ # must not reload PSModulePath back over it before the uv installer runs.
+ - name: PSModulePath normalization unit tests
+ shell: pwsh
+ run: pwsh -NoProfile -File tests/studio/test_psmodulepath_normalization.ps1
+
# The guard is self-hosting, so launching it from powershell.exe covers
# Windows PowerShell 5.1: the shell the README one-liner lands in, handled
# nowhere else in CI, and it differs from pwsh 7 on native stderr.
diff --git a/install.ps1 b/install.ps1
index 7d7e66b8743..50363360339 100644
--- a/install.ps1
+++ b/install.ps1
@@ -20,6 +20,35 @@ function Install-UnslothStudio {
$ErrorActionPreference = "Stop"
$script:UnslothVerbose = ($env:UNSLOTH_VERBOSE -eq "1")
+ # Same fix as studio/setup.ps1, for the same reason. This script also runs
+ # astral's uv installer in-process (Invoke-Expression, below), and that
+ # installer calls Get-ExecutionPolicy out of Microsoft.PowerShell.Security.
+ # The desktop app reaches here as Tauri -> Rust -> powershell.exe
+ # (studio/src-tauri/src/install.rs), and PowerShell only rewrites
+ # PSModulePath for a direct pwsh -> powershell.exe hop, so the Rust process
+ # in between leaves Windows PowerShell 5.1 leading with PowerShell 7's
+ # module directories and unable to load its own copy of that module.
+ #
+ # Not restored afterwards, deliberately. $env: is the process environment,
+ # so running this script in an interactive console leaves the reordering in
+ # place for that session. A try/finally would not change that for the case
+ # it is raised about: the interactive path ends by running Studio in the
+ # foreground, so the finally would not fire until the user stops the server.
+ # Narrowing the trigger instead would risk skipping the fix on some chain
+ # this list does not anticipate, and the cost of that is the install failing
+ # outright, against a session-lived module precedence change here.
+ if ($PSVersionTable.PSEdition -ne 'Core' -and $env:SystemRoot) {
+ $_UnslothSystemModules = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\Modules'
+ if (Test-Path $_UnslothSystemModules) {
+ # Prepended: the problem is precedence, not absence.
+ $_UnslothKept = @(
+ $env:PSModulePath -split ';' |
+ Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) }
+ )
+ $env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';'
+ }
+ }
+
# ── Tauri structured output ──
function Write-TauriLog {
param([string]$Tag, [string]$Message)
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index e5c8162eea3..a56d49362ae 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -23,6 +23,44 @@ $ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PackageDir = Split-Path -Parent $ScriptDir
+# `unsloth studio update` spawns powershell.exe, which is Windows PowerShell 5.1,
+# and the child inherits the caller's PSModulePath. Launched from a PowerShell 7
+# prompt that path leads with PowerShell 7's module directories, which ship their
+# own Microsoft.PowerShell.Security. 5.1 finds that copy first and cannot load it:
+#
+# The 'Get-ExecutionPolicy' command was found in the module
+# 'Microsoft.PowerShell.Security', but the module could not be loaded.
+#
+# astral's uv installer calls Get-ExecutionPolicy, and the run ends there with
+# exit 1 and no further output. The try/catch around that call does not help,
+# because Invoke-Expression runs the installer in this process.
+#
+# Prepended, not appended: the problem is precedence, not absence. Clearing the
+# variable so 5.1 rebuilds its default does not help either, because the
+# machine-level value on the windows-latest image also leads with PS7.
+#
+# PowerShell rewrites PSModulePath only for a direct pwsh -> powershell.exe hop,
+# so any intermediate process defeats it (PowerShell/PowerShell#18681 is this
+# exact chain through Python). install.ps1 carries the same block for the same
+# reason; scripts/uninstall.ps1 needs none, as it loads no Security cmdlet.
+#
+# Not restored afterwards, deliberately. $env: is the process environment, so
+# running this script in an interactive console leaves the reordering in place
+# for that session. Narrowing the trigger to detect the broken chain would risk
+# skipping the fix on a chain this list does not anticipate, and the cost of
+# that is the install failing outright, against a session-lived module
+# precedence change here. See the matching note in install.ps1.
+if ($PSVersionTable.PSEdition -ne 'Core' -and $env:SystemRoot) {
+ $_UnslothSystemModules = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\Modules'
+ if (Test-Path $_UnslothSystemModules) {
+ $_UnslothKept = @(
+ $env:PSModulePath -split ';' |
+ Where-Object { $_ -and ($_ -ne $_UnslothSystemModules) }
+ )
+ $env:PSModulePath = (@($_UnslothSystemModules) + $_UnslothKept) -join ';'
+ }
+}
+
# --------------------------------------------------------------------------
# Maintainer-editable defaults
# Change these in the GitHub-hosted script so users get updated defaults.
@@ -104,7 +142,12 @@ function Refresh-Environment {
foreach ($level in @('Machine', 'User')) {
$vars = [System.Environment]::GetEnvironmentVariables($level)
foreach ($key in $vars.Keys) {
- if ($key -eq 'Path') { continue }
+ # PSModulePath joins Path as an exception. Reloading it from the
+ # registry would undo the normalization at the top of this file,
+ # and several callers of this function run before the uv installer
+ # (which loads Microsoft.PowerShell.Security), so the module path
+ # would be broken again exactly where it has to be right.
+ if ($key -eq 'Path' -or $key -eq 'PSModulePath') { continue }
Set-Item -Path "Env:$key" -Value $vars[$key] -ErrorAction SilentlyContinue
}
}
diff --git a/tests/studio/test_psmodulepath_normalization.ps1 b/tests/studio/test_psmodulepath_normalization.ps1
new file mode 100644
index 00000000000..a5ff619ed46
--- /dev/null
+++ b/tests/studio/test_psmodulepath_normalization.ps1
@@ -0,0 +1,83 @@
+#!/usr/bin/env pwsh
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# Unit test for the PSModulePath normalization in studio/setup.ps1 and install.ps1.
+#
+# Windows PowerShell 5.1 cannot load its own Microsoft.PowerShell.Security when it
+# inherits PowerShell 7's PSModulePath, which happens whenever a process sits
+# between pwsh and powershell.exe (PowerShell/PowerShell#18681). Astral's uv
+# installer calls Get-ExecutionPolicy out of that module, so the install dies.
+#
+# Two properties have to hold together, and the second is easy to lose: the
+# system module directory must be PREPENDED (appending still finds the PS7 copy
+# first), and Refresh-Environment must not reload PSModulePath from the registry
+# afterwards, because most of its callers run before the uv installer.
+#
+# Run: pwsh -NoProfile -File tests/studio/test_psmodulepath_normalization.ps1
+
+$ErrorActionPreference = "Stop"
+$repoRoot = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path
+$setupPath = [System.IO.Path]::Combine($repoRoot, "studio", "setup.ps1")
+$installPath = [System.IO.Path]::Combine($repoRoot, "install.ps1")
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+function Get-Ast($path) {
+ $tokens = $null; $errors = $null
+ $ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$tokens, [ref]$errors)
+ if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "$path has parse errors" }
+ return $ast
+}
+
+Write-Host "the normalization block prepends, in both entry points"
+foreach ($pair in @(@{ Name = "studio/setup.ps1"; Path = $setupPath }, @{ Name = "install.ps1"; Path = $installPath })) {
+ $ast = Get-Ast $pair.Path
+ $text = $ast.Extent.Text
+ Check "$($pair.Name) parses" $true
+ Check "$($pair.Name) guards on PSEdition" ($text -match "PSVersionTable\.PSEdition -ne 'Core'")
+ Check "$($pair.Name) targets the 5.1 system module dir" ($text -match "System32\\WindowsPowerShell\\v1\.0\\Modules")
+ # Prepended, not appended: @($sys) + $kept, never $kept + @($sys).
+ Check "$($pair.Name) puts the system dir first" (
+ $text -match '\(@\(\$_UnslothSystemModules\)\s*\+\s*\$_UnslothKept\)'
+ )
+ Check "$($pair.Name) does not append it instead" (
+ -not ($text -match '\(\$_UnslothKept\s*\+\s*@\(\$_UnslothSystemModules\)\)')
+ )
+}
+
+Write-Host "Refresh-Environment leaves PSModulePath alone"
+$setupAst = Get-Ast $setupPath
+$fn = $setupAst.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq "Refresh-Environment"
+}, $true)
+Check "exactly one Refresh-Environment" ($fn.Count -eq 1)
+$fnText = $fn[0].Extent.Text
+Check "it skips PSModulePath as well as Path" ($fnText -match "\`$key -eq 'PSModulePath'")
+
+# Behavioural: the registry reload must not clobber a value already normalized.
+# On non-Windows GetEnvironmentVariables('Machine') is empty, so this leg only
+# proves the function is callable there; the AST check above is what holds the
+# line cross-platform.
+$savedPath = $env:Path
+$savedModulePath = $env:PSModulePath
+try {
+ Invoke-Expression $fnText
+ $sentinel = "C:\__unsloth_sentinel__;C:\Windows\System32\WindowsPowerShell\v1.0\Modules"
+ $env:PSModulePath = $sentinel
+ Refresh-Environment
+ Check "a normalized PSModulePath survives a refresh" ($env:PSModulePath -eq $sentinel)
+} finally {
+ $env:Path = $savedPath
+ $env:PSModulePath = $savedModulePath
+}
+
+Write-Host ""
+if ($failures -gt 0) {
+ Write-Host "Results: $failures failed" -ForegroundColor Red
+ exit 1
+}
+Write-Host "Results: all passed"