PowerShell、pwsh与PowerShell ISE的[type]行为异常差异排查求助
PowerShell中
[System.Windows.WindowState]类型无法识别的排查方向 问题现象
此前PowerShell 5(powershell.exe)、PowerShell 7(pwsh.exe)和PowerShell ISE 5(powershell_ise.exe)中使用[System.Windows.WindowState]指定类型的行为一致,但目前仅PS ISE 5能正常返回类型信息,另外两个环境即使添加-noprofile参数启动,仍提示类型未找到:
PSv5 执行结果
(PSv5) C:\Users> [System.Windows.WindowState] Unable to find type [System.Windows.WindowState]. At line:1 char:1 + [System.Windows.WindowState] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Windows.WindowState:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound
PSv7 执行结果
(PSv7) C:\Users> [System.Windows.WindowState] InvalidOperation: Unable to find type [System.Windows.WindowState].
PS_ISEv5 执行结果
(PS_ISEv5) C:\Users> [System.Windows.WindowState] IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True WindowState System.Enum
排查方向
- 手动加载目标程序集:在PSv5和PSv7中执行
Add-Type -AssemblyName PresentationFramework,之后再测试[System.Windows.WindowState]是否能正常识别。PS ISE会默认加载WPF相关程序集,而普通控制台环境不会自动加载。 - 检查程序集完整性:
- 对于PSv5,确认
C:\Windows\Microsoft.NET\Framework\v4.0.30319\PresentationFramework.dll存在且未损坏,可尝试以管理员权限执行regsvr32 PresentationFramework.dll重新注册。 - 对于PSv7,确认对应版本的.NET Desktop Runtime下的
PresentationFramework.dll(路径示例:C:\Program Files\dotnet\shared\Microsoft.WindowsDesktopApp\7.0.xx\PresentationFramework.dll)存在,若缺失可修复或重新安装.NET Desktop Runtime。
- 对于PSv5,确认
- 排查执行策略限制:执行
Get-ExecutionPolicy查看当前策略,临时设置为RemoteSigned(Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force)后再次测试,排除策略阻止程序集加载的可能。 - 排查系统更新影响:查看近期是否安装了Windows或.NET相关更新,尝试卸载最近的更新后验证问题是否消失。
- 测试干净用户环境:创建新的本地用户账户,在该账户下启动PowerShell测试类型识别,排除当前用户配置或权限异常导致的问题。
内容的提问来源于stack exchange,提问作者Bill Riedy
相关产品推荐
相关产品推荐

