'How to delete certain file type on multiple servers via powershell

I'm trying to remove all .pfx files on multiple servers in my environment. I've tried using the following script but it doesn't seem to work and it doesn't spit out an error.

$Systems = Get-Content "$($env:USERPROFILE)\desktop\SystemList.txt"
foreach ($System in $Systems){
Remove-Item "\\$($System)\C$\*.pfx"}

I've tried changing the file type to a test file "nike.txt"

$Systems = Get-Content "$($env:USERPROFILE)\desktop\SystemList.txt"
foreach ($System in $Systems){
Remove-Item "\\$($System)\C$\nike.txt"}

It then gives me this error

   Remove-Item : Cannot find path '\\xxxxxx\C$\nike.txt' because it does not exist.
  

I'm thinking that I have to narrow down my directory to exactly where the file sits at for this script to work but is there a way to remove all PFX file type just by searching the whole C: via powershell?



Solution 1:[1]

Try finding the items from the root of the C: drive

$files = Get-ChildItem \\$System\C$ -Filter *.pfx

foreach($file in $files) {
   Remove-Item $file.FullName
}

It may not be the fastest way but it will work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ernest Correale