如何在powershell中删除用户配置文件?

axzmvihb  于 2023-02-16  发布在  Shell
关注(0)|答案(2)|浏览(554)

我正在编写一个powershell脚本来删除用户配置文件,我知道我使用的方法不是最好的。我想知道什么是更好的方法来做到这一点?我仍然是非常新的powershell,但我愿意学习
我已经拥有的代码:

$ErrorActionPreference= 'silentlycontinue'
$Users = Get-WmiObject -Class Win32_UserProfile
$IgnoreList = "helpdesk", "administrator", "Default"

:OuterLoop
foreach ($User in $Users) {
    foreach ($name in $IgnoreList) {
        if ($User.localpath -like "*\$name") {
            continue OuterLoop
        }
    }

    $User.Delete()
}
wgxvkvu9

wgxvkvu91#

当企业方法是GPO时,为什么要编写此脚本呢?
How to Delete Old User Profiles Using GPO and PowerShell?
没有理由从头开始做这个,leverage what others have provided...
Use PowerShell to remove local profiles
How to delete user profiles older than a specified number of days in Windows
...以及MS www.example.com中的模块powershellgallery.com,您可以查看您决定使用的任何方法。

Find-Module -Name '*user*profile*' | Format-Table -AutoSize

<#
# Results

Version Name                               Repository Description
------- ----                               ---------- -----------
1.0     UserProfile                        PSGallery  This module manages user profiles on local and remote computers
0.1.1   Microsoft.Graph.Users.ProfilePhoto PSGallery  Microsoft Graph PowerShell Cmdlets
1.0.6   Get-UserProfile                    PSGallery  The Get-UserProfile module list or remove User Profiles from local
#>

Find-Module -Name '*userprofile*' | Format-List -Force

更新
但你明确说过...
我知道我用的方法不是最好的,我想知道什么是更好的方法?
...我们都建议,使用GPO是最好的方法,也是业界公认的企业级方法。除非别无选择,否则不要编写脚本。Windows AD将为您完成此任务。
不要重新发明轮子,除非你知道它真的是一个更好的轮子。在学习中,当然,有学习,试验和错误,但学习和使用的来源已经这样做了。有大量的例子遍布网络。只是搜索它。没有理由从头开始。
'powershell remove user profiles'
这显示了您已经在做什么...示例-通过Mspowershellgallery.com为该用例预构建的脚本。
Use PowerShell delete a user profile (step-by-step guide)

Get-CimInstance -ComputerName SRV1,SRV2,SRV3 -Class Win32_UserProfile | 
Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } | 
Remove-CimInstance

Remove-UserProfile - Remove Local User Profiles and Clean C:\Users Directory
此脚本包含一个函数(Remove-UserProfile),用于删除用户配置文件以及本地计算机上C:\Users目录(如果指定)的其他内容。
下载地址:Remove-UserProfile.ps1
Delete Unused user Profiles on local machine (PowerShell)
Script Delete user profiles over multiple servers v2
使用你从上面的命令中看到的模块,不是以后要做的事情。这些模块在MS和PowerShell中直接存在/可用,这是有原因的。你在PowerShell中使用的所有模块都来自你机器上托管的模块,以及你从MS和其他资源下载和安装的模块。
同样,请使用Windows或其他选定操作系统中的内置企业级工具,如果它们不能提供您所需要的功能,请考虑其他选项,例如编写企业级工具未在其GUI中公开的对象级脚本。

yi0zb3m4

yi0zb3m42#

我也做过类似的事情,我发现我需要等待cpu的稳定,因为appxsvc服务会无限制地产生线程,我曾经删除过每个用户的防火墙规则,但是现在似乎没有必要了。

$excludedprofilelist = 'C:\Users\admin1','C:\users\admin2'

$myprofiles = $profiles | where { !$_.Special -and
  $excludedprofilelist -notcontains $_.LocalPath }

$sleepseconds = 1
$numcores = 4

foreach ($profile in $myprofiles) {
  $msg = "deleting profile " + $profile.LocalPath
  $profile | remove-wmiobject
  $msg = $msg + " $?"
  echo $msg   # the result

  # recycle bin
  if (test-path c:\`$recycle.bin\$($profile.sid)) {
    rm -r c:\`$recycle.bin\$($profile.sid) -force
  }

  # wait for appx cleanup, what if profile delete error?
  #while ( (get-appxpackage -user $profile.sid).count ) {
  #  sleep 1
  #}
  do {
    # make sure it's running
    $id = Get-WmiObject -Class Win32_Service -Filter "Name = 'appxsvc'" | 
      Select-Object -ExpandProperty ProcessId

    # $proc = get-process -id $id
    # access is denied
    # 4proc.priorityclass = 'belownormal'
  
    $cpu1 = (get-process -Id $id).cpu
    sleep $sleepseconds
    $cpu2 = (get-process -Id $id).cpu
    $cpu = [int](($cpu2 - $cpu1)/($numcores*$sleepseconds) * 100)
  } while ($cpu)
}

相关问题