Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files defined in an array with Powershell

Tags:

powershell

Is it possible to define an array of filenames (all files in different folders) and then in a loop delete them all, or do something else?

Actually I need to create a few symbolic links using mklink to one file, putting those links in a different folders, replacing the old links if there was any.

like image 773
iLemming Avatar asked Oct 13 '10 17:10

iLemming


People also ask

How do I Remove files from a directory in PowerShell?

Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output. Here, this command will delete a file called “testdata. txt”.

How do I Remove an element from an array in PowerShell?

Use the Remove() Method to Remove Item From an ArrayList in PowerShell. The following example creates an ArrayList $days with items Sunday , Monday , and Tuesday . You can use the Get-Type() method to view the current data type of a variable.

How do you delete a file in PowerShell script?

Cmdlet. Remove-Item cmdlet is used to delete a file by passing the path of the file to be deleted.


2 Answers

Deleting an array of filenames is simple:

Remove-Item foo.txt,c:\temp\bar.txt,baz\baz.txt

Or via a variable:

$files = 'foo.txt','c:\temp\bar.txt','baz\baz.txt'
Remove-Item $files

And then based on all files in different folders:

$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r | Where {!$_.PSIsContainer} | Remove-Item -WhatIf

Remove the -WhatIf to do the actual removal.

If you want to delete a file with a specific name you could use the -Filter parameter on Get-ChildItem. This would be the best performing approach:

$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r -filter foo.bak | Remove-Item -WhatIf

If the name requires more sophisticated matching then you can use a regex in a Where scriptblock e.g.:

$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r | Where {$_.Name -match 'f\d+.bak$'} | 
                            Remove-Item -WhatIf
like image 143
Keith Hill Avatar answered Sep 23 '22 19:09

Keith Hill


something like this should work: can't test right now, sorry

$filenames = @('filename1.txt', 'filename2.txt', 'filename3.txt')

foreach($file in $filenames)
{
   #GCI recursive to find all instances of this filename
   $filesToDelete = Get-ChildItem -R | where {$_.Name -eq $file}

   foreach($f in $filesToDelete)
   {
      #delete the file that matches, etc. here
      # just using Write-Host to echo out the name for now
      Write-Host $f.Name
   }

}

As with most powershell, you can really compress this, but wanted to extend for explanation.

You could extend this to match your needs. For example if you needed all files that contain the word "delete", you could do gci | where {$_.Name -like "$file"}

like image 45
Taylor Bird Avatar answered Sep 25 '22 19:09

Taylor Bird