0

I'm working on an upgrade to an application and I need to delete all of the files in several folders so that the the files are regenerated when users login. The file path is Master#(where # is the workspace no. in the app)\MR\Homepage. The issue is, there are 136 MASTER folders, so I wanted to see if there's a way to run a script to go into each of these folders and delete all of the files.

So to summarize it, I want to have this script go into each db\Master#\MR\Homepage folder and delete all of the contents. I know how to do this for a single folder but not for each folder through the same script.

Remove-Item X\db\MASTER1\MR\Homepage\*.txt* -Include *.txt.ycache* -Confirm$false

Is there a way to wildcard the MASTER# in the url link? I don't want to delete anything other than what's in the Homepage folder.

Just as an update, this is on TEST, I am not touching Production. The files I'm removing are not system files and removing them does nothing but cause a new file to be generated with updated settings when a user logs in. I am now using the following, replacing the # for each directory I need to update.

new-item "N:\11.6.12 Update\Homepage Backups\Homepage35" -ItemType Directory

dir "X:\db\MASTER35\MR\Homepage*" | mv -dest "N:\11.6.12 Update\Homepage Backups\Homepage35" -verbose

2
  • My last comment wasn't as nice as it should have been. Deleting all files in certain locations can backfire quickly (I KNOW, I HAVE DONE IT MORE THAN ONCE). Be careful please. Running your code on someone elses machine comes with big responsibility. Commented Apr 30, 2021 at 14:50
  • I agree completely about being careful. I'm backing up everything and these aren't system files but files that define the layout of a user's homepage in each workspace of our ITSM tool. When one is deleted, if that user logs back into the environment a new one is generated using the updated changes that the vendor made for us. If we don't remove them, it retains the old settings/dashboard. new-item "N:\11.6.12 Update\Homepage Backups\Homepage35" -ItemType Directory dir "X:\db\MASTER35\MR\Homepage\*" | mv -dest "N:\11.6.12 Update\Homepage Backups\Homepage35" -verbose
    – ptmc518
    Commented Apr 30, 2021 at 16:02

1 Answer 1

0

I haven't actually tried it, but you may be able to use a wild card at the ...\MASTER*\.... However, assuming that you can't, what you can do is

1..136|ForEach-Object {Remove-Item X\DB\Master$_\MR\HomePage\*.txt* -Include *.txt.ycache* -Confirm:$false

However, deleting with wildcards is fraught with peril; you should be very sure that you are not potentially deleting files that you don’t intend to

1
  • Most definitely. This is only on a Test instance for now and everything is of course backed up before we do anything. Thanks for your response, I really appreciate the help.
    – ptmc518
    Commented Apr 30, 2021 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .