删除本地用户主目录 powershell [英] Delete Local User Home Dir powershell

查看:103
本文介绍了删除本地用户主目录 powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function deleteUsers($hash) {
   foreach ($h in $hash.GetEnumerator()) {
    Try
    {
        Remove-LocalUser -Name $($h.Name)
    }
    Catch{
        "Can't Delete User {0}" -f $($h.Name)
    }
   }
}

function createUsers($hash) {
   foreach ($h in $hash.GetEnumerator()) {

    $Password = ConvertTo-SecureString $($h.Value) –AsPlainText –Force
    New-LocalUser -Name $($h.Name) -Password $Password -AccountNeverExpires -FullName $($h.Name) -PasswordNeverExpires -UserMayNotChangePassword
    Add-LocalGroupMember -Group "Users" -Member $($h.Name)
   }
}


$users = @{"User blabla" = "pass"; 
           "User blabla2" = "pass2"
        }

createUsers($users)
deleteUsers($users)

这个基本的 powershell 工作正常,但根本不会删除用户主目录,我应该向 deleteUsers 函数添加什么来解决这个问题?我找不到让 Get-LocalUser 发生的简单方法.我只看到 Get-ADUser 的解决方案:/

This basic powershell works fine but simply doesn't delete the user home directories, what should i add to deleteUsers function to fix this? I can't find an easy way to make it happen for Get-LocalUser. I only see solutions with Get-ADUser :/

我想要一个与下面相同类型的解决方案

I would love a solution on the same kind as below

$homeDir = Get-LocalUser -Name $($h.Name) -Properties HomeDirectory | Select -ExpandProperty HomeDirectory

    If (Test-Path $homeDir) 
    {
        Remove-Item -Path $homeDir -Force
    }

非常感谢

推荐答案

我不建议将配置文件路径构建为 "C:\\Users\\{0}" -f $h.Name 然后通过该路径过滤 Win32_UserProfile .不保证用户的个人资料将始终位于 C:\Users\.一般来说,更好的方法是:

I wouldn't recommend constructing the profile path as "C:\\Users\\{0}" -f $h.Name and then filtering Win32_UserProfile by that path. It's not guaranteed that a user's profile will always reside in C:\Users\<username>. Generally a better approach is:

  1. 确定用户的 SID:

  1. Determine the user's SID:

$name = 'someuser'
$fltr = "name='${name}' and domain='${env:computername}'"
$sid  = Get-WmiObject Win32_UserAccount -Filter $fltr |
        Select-Object -Expand SID

$name = 'someuser'
$acct = New-Object Security.Principal.NTAccount($name)
$sid  = $acct.Translate([Security.Principal.SecurityIdentifier]).Value

  • 使用 SID 查找和删除配置文件:

  • Use the SID to find and delete the profile:

    Get-WmiObject Win32_UserProfile -Filter "sid='${sid}'" | ForEach-Object {
        $_.Delete()
    }
    

  • 删除帐户:

  • Delete the account:

    Remove-LocalUser -Name $name
    

    或(如果您运行的是旧版 Windows)

    or (if you're running an older versin of Windows)

    ([adsi]'WinNT://.').Delete('user', $name)
    

  • 按照这个顺序.

    如果您已经删除了一个帐户并且需要删除孤立的配置文件,您可以过滤 Win32_UserProfile 以查找引用计数为零的配置文件:

    If you already deleted an account and need to remove the orphaned profile you can filter Win32_UserProfile for profiles with a reference count of zero:

    Get-WmiObject Win32_UserProfile -Filter 'refcount=0' | ForEach-Object {
        $_.Delete()
    }
    

    另外,请注意 $profile 是一个 自动变量 带有您的PowerShell 配置文件,因此您不应将该变量用于其他用途.

    Also, note that $profile is an automatic variable with the path to your PowerShell profile, so you shouldn't use that variable for other things.

    这篇关于删除本地用户主目录 powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆