Hi there, I am a non coder/scripter and have been tasked with writing a deletion script that deletes files from a shared network drive specified by file path and collect a log of success/fails.
Limitations:
I have no super user access to all of our network drive locations, so will be getting the end user to run individual copies of the script in their domain of access (e.g. Z:\Finance, Z:\Operations etc)
Staff use a mix of Win 10 and 11.
Mostly from network drives that could be mapped to any letter on the individual users computer.
I need to:
Add the users particular set of files to a template copy of the script (anywhere between a dozen or thousands),
Share it with them over Teams,
Talk them through executing it; and
Have them send me the log output confirming status of deletions.
Iāve tried writing this myself and just failed again and again. I asked copilot to help a brother out and it provided some impressive looking non-functional code. At my skill level right now, I canāt even begin to identify whatās wrong with it.
What I would like is to be able to paste the following into a script that will capture the output:
Remove-Item -Path D:\test\passports.xlsx -Force
Remove-Item -Path D:\test\medical certificate.pdf -Force
Remove-Item -Path D:\test\sensitive personal information.msg -Force
Remove-Item -Path D:\test\interview notes.docx -Force
Remove-Item -Path D:\test\contract.docx -Force
[some code here that catches success/failure status and writes it to a text file to be saved on their local drive]
I can already hear some questions as to why it would be done this way. I am open to hearing better but here is my rationale for some of the limitations up front:
The range of staff I will be asking to run the script is varied and very non-technical. If they could just execute the script without any extra steps, this would be ideal. I donāt want to talk them through saving a csv of filepaths anywhere or making sure the output folder exists.
My organisation will not give me access to all the network share domains (maybe they even canāt - itās managed by a third party). I am expected to go business unit by business unit to get this done.
I canāt use any scripting to identify files for deletion. Identifying still requires the old fashion version of fuzzy logic (human eyeballs) to confirm deletion is ok, so list needs to be produced first and injected into the script
I would very much appreciate some ideas to get me going with this, Iām totally stuck. I can delete files en masse but I cannot log having done it, particularly when it fails due to admin rights or the specified path and file dont exist.
can you expand a bit more on how the targeted files are to be identified? Is the individual user providing that input? Are they to target some folders by name or are there certain file names theyāre looking for?
Thanks grey0ut. Files are identified a couple of different ways. By filename for the most part. We also have a scanning solution that scans file contents for certain keywords and regex patterns.
High confidence results are accepted, middle confidence results are reviewed manually.
Our scanning solution is presenting a lot of false positives at the moment so the manual review is still key.
Ok, so how does that look for the script then? Is each userās computer going to have an input file the script uses to get its file list from? Are the users going to be expected to select the files themselves via the script?
I have an idea for one way you could handle logging. Iāll edit this comment when I get to a computer.
EDIT:
Ok assuming $FilesToDelete is an array full of strings, with each string representing a file to delete. Letās use your example
I didnāt test this, so grain of salt and all that, but the idea is to use a try/catch block to ācatchā any errors. Whether it succeeds or fails spit out a PSCustomObject that weāre capturing in the $Results array so we can output that later.
The āRemovedā property will be a simple boolean so youāll know if the file was successfully removed or not. If it fails, the third property āErrorā should contain whatever the error text was (admin rights, file not found etc).
I hope this helps in some way.
I really wanted to be able to avoid an input script, so I could just send them 1 file to execute. So like the example I posted to literally just have all files marked for deletion as line items in the script, although I am concerned about how well that works if there are thousands of them. I have done as many as 12000 using a BAT file (literally just .del āC:\something.txtā. and it just smashed through them, hoping PS would be more or less the same. If that is too inefficient for the script then input file it is. Iām kind of aware my needs as I state them doesnāt lend themselves to best practice.
After trying to solve this a few different ways I really want to have minimal requirements to the user. Over teams chats I have watched people struggle to create a temp folder in their C drive to store a log and input file. I donāt want to disparage anyones abilities (I am here seeking help after all), but I would like to circumvent the different skill levels of people involved.
If my own skills were better I would try an approach such as having the input list in a sharepoint library and share access to the user and have the output log emailed to me (this last Iāve seen code for in other languages).
Right now I would settle for the simpler approach of executing a pretty blunt deletion and asking the user to email me the log.
Iām afraid Iām all too aware of whatās possible without having the skills to execute
Why would you send them anything to run? If you have the list of files to delete, they should have no impact or even knowledge of the script running. You can set up a login script that references specific list for files to delete. Another option is to run the script through scheduled tasks. You donāt even have to target specific users, you can just set it to run as āusersā and any user on any given computer will run their own instance of the script. Honestly, I think this is a bad idea altogether and I would be pushing for a centralized single point to perform the file deletions. You said you donāt have āsuper adminā but assuming these shares are stored on servers, there are many different ways you can accomplish your task. Just my 2 cents
Canāt disagree, but Iām not part of our ICT department, I can only delete what I personally have permissions to delete. If I need to delete 1000 files from networkdrive:\Operations and Iām not part of that team, I canāt even open that filepath let alone RWXD.
We have certainly pushed for a single centralised point to perform these actions but our SteerCo has declined to sponsor that at this point. Itās a large organisation with a lot of politics and territorial disputes
There has been negotiations going on for a vendor solution with ICT, itās been a year and we donāt appear to be close to that. Down below the rarefied air of decision makers, we are nonetheless expected to carry on and produce results and report on progress. What weāre hitting with this solution is low hanging fruit and thereās plenty of it.
This is not a final state weāre developing with deletion scripts, this is just keeping things warm until stakeholder consensus lets us move forward with a better solution⦠probably sometime after the second coming if I reflect on the inertia in our organisation
If youāre about to simplify or speed up the work some of yours colleagues have to do you will save your company a lot of money. How about talking to the responsible people and get you the parmissions and ressources you need to accomplish this task?
Even better, the code is clear and easy to understand , I am learning!
I added 2 more lines to the script, first line to create the destination folder of output if it does not exist (I think filepath or folder name is a bit different in Windows 10) and the last line to open that folder when complete.
This worked fine here on my local computer, any reason why this could be problematic out in the wild?
The additions look like they should work. I would avoid using aliases like āiiā in production code because itās difficult to read. This one is a good example because I had no idea what it was. I popped open Powershell and typed Get-Alias 'ii' in order to see that this is for Invoke-Item.
The only issue I can see is if for some reason the original creation of the āC:\Tempā folder fails, the logs will fail to export there, the script execution will complete, and the logs will be lost forever.
You might consider using a folder thatās likely already present, depending on your environment. The Desktop or Documents folder maybe?
If you take a look at the automatic variable $home on your machine youāll see that itās the home folder for the current user. If your user is running the script, then itāll be their home folder and theyāll for sure have rights to it.