Search code examples
powershellopencsv

delete users in power shell using a csv file (What I am missing?)


Well now I am trying this

ForEach ($name in $list){ Get-CimInstance -ComputerName oa-exwit -ClassName Win32_UserProfile | Where-Object { $_.LocalPath.split('')[-1] -eq $name } | Remove-CimInstance

but nothing happens, no errors no deleted user, nothing


Solution

  • #"C:\Users\caro\script.ps1" command to run the script #list is a variable array that connects to the computer and collects the profiles #users is a variable array that collects the users to be deleted #first loop interact the users array and ask that for each name do interact in the second loop that contains the profiles array; #once the loop check if there is a coincidence between the first loop and second loop it deletes the instance

    $list= Get-CimInstance -ComputerName oa-exwit -Class Win32_UserProfile $users= Get-Content file.csv

    ForEach($user in $users){

    ForEach($name in $user){ 
            
        ForEach($profile in $list) {
            if ($name -eq $profile.LocalPath.split('\')[-1]){
            Remove-CimInstance($profile)
            }
        else{
        continue
        }
    }
    }
    

    }