You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell删除本地用户报错:调用Delete时参数不足

本地AD用户副本删除报错及解决办法

问题现象

开发工具删除笔记本电脑中AD用户的本地副本时,执行删除操作触发如下错误:

Exception calling "Delete" with "0" argument(s): ""

改用Remove-WMIObject执行删除,同样会出现类似报错。

涉及代码

报错的删除逻辑代码

$UserProfile = Get-WmiObject Win32_UserProfile -filter "LocalPath Like 'C:\\users\\$str'"
$UserProfile.Delete()  

变量$str的定义代码

Get-WMIObject -ClassName Win32_UserProfile -Filter "special=false and localpath like 'C:\\users\\%'" | ForEach-Object {
    # 内部逻辑已简化,仅检查用户前期选择,匹配时将$target设为当前对象
    $target = $_    
}
$str = $target.localpath.TrimStart("C:\Users\")

修复方案

原逻辑为:先获取WMI对象截取用户名用于展示,再通过该用户名重新查找WMI对象执行删除——虽明知重复获取对象冗余,但因无法定位报错原因才采用此方式。

最终解决方法:在获取WMI对象的命令中添加-ComputerName localhost参数,即便此前无需该参数也能正常获取WMI对象。

修复后的示例代码

$UserProfile = Get-WmiObject Win32_UserProfile -ComputerName localhost -filter "LocalPath Like 'C:\users\$str'"
$UserProfile.Delete()

内容的提问来源于stack exchange,提问作者Bobbity

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:02:44