Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中删除用户配置文件?_Powershell_User Profile - Fatal编程技术网

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

如何在powershell中删除用户配置文件?,powershell,user-profile,Powershell,User Profile,我正在编写一个powershell脚本来删除用户配置文件,我知道我使用的方法不是最好的。我想知道有什么更好的方法?我对powershell仍然很陌生,但我愿意学习 我已有的代码: $ErrorActionPreference= 'silentlycontinue' $Users = Get-WmiObject -Class Win32_UserProfile $IgnoreList = "helpdesk", "administrator", "Default" :OuterLoop fore

我正在编写一个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()
}

当企业方法是GPO时,为什么要编写此脚本

没有理由从头开始做这件事

…以及MS 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

此脚本包含一个函数(Remove UserProfile),用于 删除用户配置文件和C:\Users的其他内容 本地计算机上的目录(如果指定)

下载:

使用从上面的命令中看到的模块,不是以后要做的事情。由于某种原因,MS和PowerShell直接提供了这些功能。您在PowerShell中使用的所有内容都来自您计算机上托管的模块,以及您从MS和其他资源下载和安装的模块


同样,在Windows或其他设计的操作系统中使用内置的企业工具,如果它们不能提供您所需要的,那么请使用其他选项,例如编写企业工具在GUI中未公开的对象级别的脚本。

为什么您认为这不是最好的方法?最多,我可能会添加一些检查,以确保您尝试删除的用户不是当前登录的用户,但使用
Win32_UserProfile
.delete()
方法实际上是正确的方法。我想不出更好的方法。哦,好的。从我所读到的内容来看,使用
Win32\u UserProfile
不是正确的方法。我该如何确保他们已注销?没关系。还有一个清理配置文件的组策略。有时候,Office Hub应用程序不起作用。我正试图用powershell“让我的脚湿透”。这是一个大学IT服务台的工作,只是为了让我的同事们更容易一点。但是谢谢!我将在以后的生活中研究这些问题。一位老同事告诉我,我使用的方法可能不是最好的方法。我记得在某个地方读到它,它不是,所以我要确定。正如我所说,我目前在一所大学的IT服务台工作,我只是一名技术员。我无法控制我们在广告中有什么政策或任何东西,所以我只是在摆弄它,让普通的任务变得更容易。我知道这不是常见的做法,当我得到一份更高的工作时,我会学习的,但现在我只是利用我所能得到的
Get-CimInstance -ComputerName SRV1,SRV2,SRV3 -Class Win32_UserProfile | 
Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } | 
Remove-CimInstance