【问题标题】:Delete local windows profile with PowerShell使用 PowerShell 删除本地 Windows 配置文件
【发布时间】:2010-09-20 21:40:44
【问题描述】:

我正在尝试编写一个脚本来删除测试帐户的本地配置文件。我正在使用以下行返回以“test-”开头的任何帐户的 SID

PowerShell:$UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).SID

获得 SID 后,我使用 wmic 进行删除,但我不确定如何将该代码转换为 PowerShell。

WMIC:wmic /node:"localhost" path win32_UserProfile where Sid="%%b" Delete

【问题讨论】:

    标签: powershell wmi


    【解决方案1】:

    我认为这会起作用,但我在 Win32_UserProfile 类上找不到删除方法

    $UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).SID
    (gwmi -class Win32_UserProfile -filter "SID='$UserSID'").Delete()
    

    【讨论】:

    • 我收到以下错误:使用“0”参数调用“删除”的异常:“”行:2 char:64 + (gwmi -class Win32_UserProfile -filter "SID='$ UserSID'").Delete
    • 我需要在您的线路中添加“-ComputerName localhost”。
    • 解决错误“使用“0”参数调用“删除”的异常:“以管理员身份运行 Powershell。
    【解决方案2】:

    您也可以直接在单个语句中调用 Delete 方法:

    (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).Delete()
    

    【讨论】:

      【解决方案3】:

      获得使用“0”参数调用“Delete”的异常的另一个原因是您尝试删除的用户当前已登录。记录他关闭并重试。

      【讨论】:

      • 以管理员身份运行 Powershell 为我解决了该错误。
      • 不如运行([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"),看看是否返回True,说明你是Administrator。
      【解决方案4】:
      Get-WmiObject Win32_UserProfile -Filter "RoamingConfigured = 'True'" | Remove-WmiObject
      

      True - 漫游配置文件
      False - 本地配置文件

      【讨论】:

        【解决方案5】:

        我通过以管理员身份打开 Powershell(右键单击,以管理员身份运行)解决了这个问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-26
          • 2016-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多