PowerShell to delete Desktop Items from a remote PC

若如初见. 提交于 2020-01-13 20:39:13

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/20667310/powershell-to-delete-desktop-items-from-a-remote-pc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!