Deleting the old user profiles in remote servers RRS feed

  • Question

  • Hi

    I want to delete the olduser profiles in the remote server. I am taking the input from csv file and after providing the path it deletes the profile in remote machines.

    But this script takes long to execute. May i know the reason. script and the csv file attached.

          [Parameter(Mandatory = $True)][string] $path
    $erroractionpreference = "silentlycontinue"  
    $a = import-csv $path
    $list = $a |?{$_.Status -eq "delete"}                                                                                    
    $Computers = $list.ComputerName
    foreach($computer in $Computers)
    $s= New-CimSession -ComputerName $computer
    $profiles = Get-CimInstance win32_userprofile -computername $Computer -Verbose 
     foreach($profile in $profiles)
        $objsid = New-Object System.Security.Principal.SecurityIdentifier($Profile.sid)
        $objuser = $objsid.Translate([System.Security.Principal.NTAccount])
        $objusername = $objuser.value
        if($objuser.value -eq $list.ProfileName)
           $incno = ($a |?{$_.Status -eq "delete" -and $_.ProfileName -eq $objusername }).IncidentNumber
           $computer1 = ($a |?{$_.Status -eq "delete" -and $_.ProfileName -eq $objusername }).ComputerName 
           $logFileExists = Get-EventLog -ComputerName $computer1 -logname System | Where-Object {$_.source -eq "UserProfile"} 
           if (! $logFileExists) {
           New-EventLog -LogName system -Source "UserProfile" -ComputerName $computer1
           $confirm = Read-Host "Are you sure want to perform this action?[y/n]"
           Remove-CimInstance -InputObject $profile -confirm -Verbose 
           if($confirm -eq "y"){
           Write-EventLog -ComputerName $computer1 -LogName system -Source "UserProfile" -EventID 101 -Message "$objusername with incno $incno got deleted." 
           Write-EventLog -ComputerName $computer1 -LogName system -Source "UserProfile" -EventID 102 -Message "$objusername with incno $incno not got deleted."
    Remove-CimSession $computer 


    Thursday, June 21, 2018 6:00 AM