Account Unknown user profile delete with out data loss in windows servers

D Vijayakumar VLR 126 Reputation points
2020-11-22T20:11:59.41+00:00

Hi All,

I have script for Account unknown user profile deletion but i need to login each and every for this clean up activity, So i want to run this script from my local client OS or my VDI to clear on all remote servers at a time and i want to know the user profile last login and log out time with date

$Computer = "DESKTOP-T20064S"
$Output = "C:\Temp\AccountUnknownProfiles\CleanUPProfiles.xls"
Get-CimInstance win32_userprofile | foreach {
Get-wmiobject win32_userprofile -Computername DESKTOP-T20064S | Select PSComputerName,LocalPath,SID,LastUseTime | Export-Csv -Path $output | format-list
$u = $_
try {
$objSID = New-Object System.Security.Principal.SecurityIdentifier($U.sid) -ErrorAction stop
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
"User={0}" -f $objUser.Value
"Path={0}" -f $u.LocalPath
"SID={0}" -f $u.SID
}
catch {
"!!!!!Account Unknown!!!!!"
"Path={0}" -f $u.LocalPath
"SID={0}" -f $u.SID

Remove-CimInstance -inputobject $U -verbose
$Count++

}

}
$PC = [System.Net.DNS] :: GetHostByName($Null) HostName
Write-Host "on" $PC $Count "Account Unknown has been deleted"

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,388 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2020-11-23T10:04:51.837+00:00

    Hi,
    You can run your script on remote computers with Invoke-Command

    Invoke-Command -FilePath $script -ComputerName $server -Credential $cred  
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. D Vijayakumar VLR 126 Reputation points
    2020-11-25T06:58:42.797+00:00

    I want to run this script from my local machine or my vdi machine to clear all account unknown user profiles on remote servers

    $Computer = "DESKTOP-T20064S"
    $Output = "C:\Temp\AccountUnknownProfiles\CleanUPProfiles.xls"
    Get-CimInstance win32_userprofile | foreach {
    Get-wmiobject win32_userprofile -Computername DESKTOP-T20064S | Select PSComputerName,LocalPath,SID,LastUseTime | Export-Csv -Path $output | format-list
    $u = $_
    try {
    $objSID = New-Object System.Security.Principal.SecurityIdentifier($U.sid) -ErrorAction stop
    $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
    "User={0}" -f $objUser.Value
    "Path={0}" -f $u.LocalPath
    "SID={0}" -f $u.SID
    }
    catch {
    "!!!!!Account Unknown!!!!!"
    "Path={0}" -f $u.LocalPath
    "SID={0}" -f $u.SID
    Remove-CimInstance -inputobject $u -verbose
    $count++
    }
    }
    $PC = [System.Net.DNS]::GetHostByName($Null) HostName
    Write-Host "on" $PC $count "Account Unknown has been deleted"

    0 comments No comments

  2. BleuOisou-3552 156 Reputation points
    2022-02-28T13:37:45.487+00:00

    There is cmd understanding unclear for Invoke-Command
    There is difficulty for client user running script to remote servers \ ...,

    there is Sign-in blocking the comment to the poster(s) ?

    0 comments No comments