Question

I know this sort of question has been asked many times, but never does anyone highlight the issue i am facing.

I have a script that will look at user profiles and mark ones that are over X amount of days as ones to delete, then remove them. Remove-Item, with -Force and -Recurse make it remove all folders/files apart from the standard NTFS junction points for all users. For these sort of folders it gets access denied. I have even tried taking ownership of user folders first - still it happens. These folders on W7 being like:

C:\Users\<NAME>\My Documents
C:\Users\<NAME>\Start Menu

No matter how i make the script it cannot delete the top level user folder. With the same account, same PC - if i just use windows explorer to right click and delete, the folder will be removed along with the sub-folders.

For the record these are the methods i have tried:

Remove-Item (with -force -recurse)
[io.directory]::delete()
$variablename.delete()

I could post the script but it is kind of irrelevant, as the bulk of it works its just these junction points.

I suppose this is my question - How do i invoke the same delete command Windows Explorer is using from within PowerShell?

Thanks in advance.

Était-ce utile?

La solution

Try this answer, it shows you how to remove symlinks and you can incorporate that into your code: : Delete broken link

Post errors if it doesn't work.

Autres conseils

For me this has to do with the read-attribute on folders set. If I create a junction, I have the habit of immediately changing the icon for it as shown in Explorer. This will set the readonly attribute of the junction which you can't change with explorer, but I can change it with the attrib command :

attrib -r /d /s Junk

where Junk is a symbolic link to a folder. After that, I can remove the folder with the 'rm'-command

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top