Remove Application Data folder using Powershell

The problem with the “Application Data” folder is not that it is a junction, but that it is:
1) marked as Hidden and System
2) it has an ACL that denies everyone ReadData
To fix both of those issues…
powershell.exe:
> #First make the folder visible
> Set-ItemProperty -Path “c:\users\someuser\Application Data” -Name Attributes -Value “Normal”
> dir c:\users\someuser
Mode LastWriteTime Length Name
—- ————- —— —-
d—- 4/21/2011 5:15 PM Application Data
d-r– 4/21/2011 5:15 PM Contacts
d-r– 4/21/2011 5:16 PM Desktop
d-r– 4/21/2011 5:15 PM Documents
d-r– 4/21/2011 5:15 PM Downloads
d-r– 4/21/2011 5:15 PM Favorites
> #Next remove the deny everyone ReadData ACE from the ACL
> $acl = Get-Acl “c:\users\someuser\Application Data”
> $acl.access
FileSystemRights : ReadData
AccessControlType : Deny
IdentityReference : Everyone
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited : True
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None
>#Remove all the deny entries (there is only one on my system)
> $acl.access | where-object {$_.AccessControlType -eq “Deny”} | Foreach-object { $acl.RemoveAccessRule($_) }
True
>#Now save the ACL back on the folder object
># If you use Set-ACL it complains because the owner can’t be System,
># So instead use the “SetAccessControl” method on the FolderInfo object (or you could change the owner on the $acl and use Set-ACL)
> (Get-Item “c:\users\someuser\Application Data”).SetAccessControl($acl)
> #Now you should be able to use Remove-Item to delete the folder
> Remove-Item “c:\users\someuser\Application Data” -Force

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?

%d bloggers like this: