PowerShell to delete Desktop Items from a remote PC

12,088

Change this, you already specify a slash in your $destination variable, you are double up @ \\c$

Remove-Item "\\$computer$DESTINATION\$file" -Recurse

otherwise, you are trying to delete this path and failing.

\\computername\\c$\Documents and Settings\All Users\Desktop\$file
Share:
12,088
software is fun
Author by

software is fun

Updated on June 17, 2022

Comments

  • software is fun
    software is fun over 1 year

    I have 200 PC that need to have some specific icons removed.

    I created a CSV file with the ComputerName (1 name per row)

    I have another file with the file name of the icon that needs to be removed from the desktops (Shortcut1.lnk, etc). This other file is also a CSV (1 file name per row).

    How can I run a PowerShell script to remove those icons. (Please note that not all computers in my CSV file maybe turned on. Some maybe off or have network issues).

    $SOURCE = "C:\powershell\shortcuts"
    $DESTINATION = "c$\Documents and Settings\All Users\Desktop"
    $LOG = "C:\powershell\logs\logsremote_copy.log"
    $REMOVE = Get-Content C:\powershell\shortcuts-removal.csv
    
    Remove-Item $LOG -ErrorAction SilentlyContinue
    $computerlist = Get-Content C:\powershell\computer-list.csv
    
    foreach ($computer in $computerlist) {
      foreach ($file in $REMOVE) {
        Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
      }
    }
    

    This is my code so far but it doesn't appear to delete the files from

    \\computername\c$\Documents and Settings\All Users\Desktop
    

    I am getting errors and warnings. The log file also doesn't seem to be creating.

    Anyway to get a report of what was deleted. what was not deleted?

  • software is fun
    software is fun almost 10 years
    Thanks. I missed it but I am getting warnings saying Remove-Item : Cannot find path '\\COMPUTERNAME\c$\Documents and Settings\All Users\Desktop\SCANNER.lnk' because it does not exist. At C:\powershell\powerdelete.ps1:11 char:16 + Remove-Item <<<< "\\$computer\$DESTINATION\$file" -Recurse + CategoryInfo : ObjectNotFound: (\\COMPUTERNAME\c$\D...top\SCANNER.lnk:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
  • Knuckle-Dragger
    Knuckle-Dragger almost 10 years
    Try wrap an if clause around a test-path statement to verify the file exists before attempting to delete. Here is the kind-of syntax serverfault.com/questions/560505/…