question

vijayakumarD-1665 avatar image
0 Votes"
vijayakumarD-1665 asked ·

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

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
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered ·

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

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

https://docs.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.

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi

I try this but it is not working properly can you provide me clear details in script that invoke command where i want to use

or

Can you provide a new script for Account unknown user profile deletion all on remote servers with out data loss and i want to run that script from local machine or local VDI machine

0 Votes 0 ·

Invoke-Command allows you to run your script on remote servers. What do you mean by not working properly? Do you see any error message?

1 Vote 1 ·

I have script it's working fine with out data loss on single server, But i want to excute the same on multiple remote servers from local machine through power shell.

0 Votes 0 ·
vijayakumarD-1665 avatar image
0 Votes"
vijayakumarD-1665 answered ·

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"

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.