简体   繁体   中英

Powershell - delete folder if name exists in text file

I have a text file with a list of folder name.

I want to delete the folders from my directory for which the name appears in the text file

Here is my try

#I get the contents of the text file

$textfile = Get-Content "C:\Users\s611284\Desktop\Dossiers.txt"

#I get the name of the folders from the directory

$Foldersname = Get-ChildItem -Path $Directory | ForEach-Object { 
        $_.BaseName 
      } 

#I get the names present in the text file and in the folders of the directory

Compare-Object -IncludeEqual $textfile $Foldersname | Where-Object SideIndicator -eq '==' 

93 / 5 000

This gives me the list of folder names present in the text file and in the directory. Now I would like to delete folders present in this list

Hope someone can help

Thank you

You could use

#I get the contents of the text file

$textfile = Get-Content "C:\Users\s611284\Desktop\Dossiers.txt"

#I get the name of the folders from the directory

$Foldersname = Get-ChildItem -Path $Directory | ForEach-Object { 
        $_.BaseName 
      } 

#I get the names present in the text file and in the folders of the directory

Compare-Object -IncludeEqual $textfile $Foldersname | Where-Object SideIndicator -eq '=='|ForEach-Object {

    $item=$_.inputobject
    remove-item $Directory\$item -Recurse  
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM