How do you delete $NTUninstall* folders on remote computers listed on a text file

rated by 0 users
This post has 4 Replies | 2 Followers

Not Ranked
Posts 3
shramj Posted: 06-30-2010 7:14 PM
I am a powersheel rookie and I need help with a powershell command to delete the C:\Windows\$NTUninstall* folders from remote computers that are listed on a text file. I have all my script stuff under C:\Scripts and I have a text file called computers.txt that has all the names of the computers on my network. I want to be able to remotely delete all the $NTUninstall* folders remotely. This is what I have so far and if someone can help figure out how to actually remove the $NTUninstall folders, I would really appreciate it. I do not know what I am supposed to use as the wildcard for the $NTUninstall* folders, I am also stuck on if I need to add the attributes for the folders first. Any help would be great, I wouldn't mind also removing the KB*.log files as well. Thanks! gc C:\Scripts\computers.txt | % { Remove-Item "\\$_\C$\Windows\$ntuninstall* -Recurse }
Top 75 Contributor
Posts 46

Delete log files:
gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "kb*.log" } | ? { !($_.PsIsContainer) } | % { ri -force }

Delete $ntuninstall folders:
gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "`$ntuninstall*" } | ? { $_.PsIsContainer } | % { ri -force }

Not Ranked
Posts 3
Thanks for your help Cruisafer03 but I still can't get it to work, this is what happens.... When trying to delete the KB Logs....... Remove-Item : Cannot find path 'C:\scripts\KB982381.log' because it does not exist. At line:1 char:113 + gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "kb*.log" } | ? { !($_.PsIsContainer) } | % { ri <<<<<<
Top 75 Contributor
Posts 46

Remove-Item appears to only be taking the filename instead of the file object.  this should fix that

gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "kb*.log" } | ? { !($_.PsIsContainer) } | % { ri -path $_.FullName -force }

Delete $ntuninstall folders:
gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "`$ntuninstall*" } | ? { $_.PsIsContainer } | % { ri -path $_.FullName -force }

Not Ranked
Posts 3
Cruisader03, your command was working but it wasn't actually deleting the directories, I had to add the -force under GCI command and then it started working, thanks again for all your help! I think alot of network admin would find this script useful. Thanks! gc C:\Scripts\computers.txt | % { gci "\\$_\c$\WINDOWS" -Filter "`$ntuninstall*" -force } | ? { $_.PsIsContainer } | % { ri -path $_.FullName -force -recurse }
Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.