Home > Blockchain >  My powershell script can't delete multiple users from csv file but can delete 1 user
My powershell script can't delete multiple users from csv file but can delete 1 user

Time:10-13

This is the script which deletes user but only if 1 entry in there in csv file. however when I entered multiples entries in csv, the script takes values of all the rows as a single entry. How do I repair it? Thanks in advance!

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
$CSVPath = "D:\Temp\userss.csv"
$CSVFile = Import-CSV $CSVPath
$web = Get-SPWeb -Identity https://................./
$DocLibsName = "Bilder der websitesammlung"
$folder = $web.Folders[$DocLibsName].SubFolders

foreach($Files in $CSVFile)
{
Write-Host "Retreiving user..." -ForegroundColor Blue
$user = $CSVFile.users
$items = $folder.Files | where {$_.Title -eq $user}
If($items.Title -contains $user)
{
$items.Delete()
Write-Host $user "deleted" -ForegroundColor DarkGreen

}
Else { 
Write-Host $user"not found" -ForegroundColor DarkRed 
}

}
Write-Host "Finished" -ForegroundColor Green

CodePudding user response:

Please try to change the code "$user = $CSVFile.users" to "$user = $Files.users".Hope it can help you.

CodePudding user response:

Try using this PowerShell code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
$CSVPath = "D:\Temp\userss.csv"
$CSVFile = Import-CSV $CSVPath
$web = Get-SPWeb -Identity https://................./
$DocLibsName = "Bilder der websitesammlung"
$folder = $web.Folders[$DocLibsName].SubFolders

foreach($Files in $CSVFile)
{
    foreach($loopItem in $Files.users) {
        Write-Host "Retreiving user..." -ForegroundColor Blue
        $user = $loopItem
        
        $items = $folder.Files | where {$_.Title -eq $user}
        If($items.Title -contains $user)
        {
            $items.Delete()
            Write-Host $user "deleted" -ForegroundColor DarkGreen
        }
        Else { 
            Write-Host $user"not found" -ForegroundColor DarkRed 
        }
    }
}
Write-Host "Finished" -ForegroundColor Green
  • Related