I have a scheduled script that outputs bunch of HTML files with static names to a remote location. I noticed, that if I have one of those files selected in Windows Explorer so that its contents are shown in Preview Pane, then Powershell cannot overwrite that file and skips updating it. This only happens if output files are in remote location. Works just fine if files are local. How do I force PowerShell to overwrite remote files in this situation? Lots of users work with those reports and if one of them leaves Windows Explorer window with one of those files highlighted overnight when the script runs, the file is not going to be updated.
Asked
Active
Viewed 344 times
2 Answers
0
- Move HTML files to webserver. You will solve your problem entirely. IIS Setup on windows server is
Next, Next, Next
. You can leave link to a new file location (https://....) in old place, so users can easily navigate to a new place. Possibly this link can be automated (not sure because of modern security standards) - Try
[System.IO.File]::Delete($path)
just before writing this file. This removes file entry from filesystem, but leaves file open for those who have it open for now. This makes your script to write to a new file with the same name. Old file exists without name (deleted) but leaves open until everyone close it. Check it actually deleted with resresh! - Try
[System.IO.File]::Move($path, $someTrashFullName)
just before writing this file. $someTrashFullName probably must be on same drive. Same asDelete
, but renames file. Some self-updating software use this strategy. File is renamed, but it's still kept open under new name. - Try replace file with shortcut to some file. You can generate files with different names and change shortcut programmatically
- HTML files that change location using js ? They read nearby JSON (generated by export script) and lookup there for a new filename. So user opens static unchanged A.html, JS inside lookups at A.json for new name and redirects user to A-2020-08-11.html. I'm not sure browsers allow reading JSON files from JS for files that opened from network drive.
- Only way left is to stop network share or\and close open files server-side.
- Maybe some fun with to disable preview in this folder \ completely?

filimonic
- 3,988
- 2
- 19
- 26
-
Delete doesn't work. This is the output: ```Exception calling "Delete" with "1" argument(s): "Access to the path '\\RemoteComputer\somepath\test.html' is denied."``` – miguello Aug 10 '20 at 23:25
-
Same with ```Move``` – miguello Aug 10 '20 at 23:27
-
Are you sure that problem is with `file is locked` ? Not sure `Locked file` gives AccessDenied error. Anyways, if you move to webserver, no problems will exist. – filimonic Aug 10 '20 at 23:28
-
Well, I'm not sure if I use right terms, but this is for sure - if file on remote shared drive is selected in Windows Explorer with Preview Pane turned on, then I can' do anything to it with PowerShell. As soon as I deselect the file (like, 5-10 seconds after), everything works just fine. – miguello Aug 10 '20 at 23:47
-
Well, if file is locked by user in such mode, you can not touch it. Think about other variants – filimonic Aug 10 '20 at 23:48
0
Try with -Force. But to me, it seems to be more a permission issue.
Remove-Item -Path '\\server\share\file' -Force

Kluk
- 126
- 6
-
Yeah, I tried that already - did not work. Apparently this is a well known issue (not only with Powershell) where a remote file will get locked by Preview Pane. And as for the Permission issue - everything works just when as soon as I remove focus from that file in Windows Explorer – miguello Aug 11 '20 at 16:20
-
Ok, I tried on my side and I cannot reproduce the error. When I delete the file located on the server, File Explorer just close the preview. Where are the files you need to delete? SMB server? If that's your case, you can try Close-SmbOpenFile. I found some help here: https://serverfault.com/questions/718875/close-locked-file-in-windows-share-using-powershell-and-openfiles – Kluk Aug 12 '20 at 08:12