I am working on a Powershell script to change the ACL of a folder (the $Recycle.Bin folder actually) but I am stuck at actually applying the permission on the folder because the Get-Item cmdlet does not find the path.
Here's (part of) the script:
$acl = Get-Acl "$env:USERPROFILE\`$Recycle.Bin"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Users",'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow')
$acl.SetAccessRule($AccessRule)
(Get-Item "$env:USERPROFILE\`$Recycle.Bin").SetAccessControl($acl)
The error I am getting is that the path cannot be found:
Get-Item : Could not find item C:\Users\TestUser\$Recycle.Bin.
At line:1 char:2
+ (Get-Item "$env:USERPROFILE\`$Recycle.Bin").SetAccessControl($acl)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\test\$Recycle.Bin:String) [Get-Item], IOException
+ FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetItemCommand
You cannot call a method on a null-valued expression.
At line:1 char:1
+ (Get-Item "$env:USERPROFILE\`$Recycle.Bin").SetAccessControl($acl)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The Get-Item
cmdlet cannot find the path but I can succesfully navigate to it using the CD
command:
PS C:\Users\TestUser> cd "$env:USERPROFILE\`$Recycle.Bin"
PS C:\Users\RestUser\$Recycle.Bin>
I suspect that it's because the $Recycle.Bin folder has a " $ " in the path but then again I believe it shouldn't be interpreted as a variable since I added the " ` " character to it.
I may be wrong about this though.
It may be good to point out that this path is actually a User Profile Disk (so a VHDX) that is mounted to the server.
How can I make it so that the Get-Item
cmdlet can actually find the path?
Thank you!
Assuming your path is correct and not in the default location C:\$Recycle.bin
, you just have to quote that path in single quotes to prevent PowerShell from trying to evaluate $Recycle
which will most probably be a non-existent variable. You can get the ACL of the recycle bin like this:
Get-Acl -Path (Join-Path -Path $env:USERPROFILE -ChildPath '$Recycle.bin')
If you want to get more info about it (like with Get-Item
), you have to add the -Force
parameter, as it is a hidden system folder:
Get-Item -Path (Join-Path -Path $env:USERPROFILE -ChildPath '$Recycle.bin') -Force