Powershell Get-Childitem behavior with variables
-
Here's something I ran into yesterday. Perhaps those more knowledgeable of Windows than I can shed some light onto what's going on, or tell me if my thinking is wrong.
Take this command,
Get-ChildItem -Path C:\ -Include *.txt -Recurse -ErrorAction SilentlyContinue
As expected, this will return any file that exists with
.txt
at the end of the filename anywhere within theC:\
directory tree. If you happen to try to list items from something you can't access, error messages are suppressed.Let's say I want to store data in a variable, so I can more easily manipulate it. I'd run this command.
$foo = Get-ChildItem -Path C:\ -Include *.txt -Recurse -ErrorAction SilentlyContinue
In my environment of Server 2012 R2, domain-joined machine, running as the local, built-in Administrator account, the first command works flawlessly, while the second command throws an "Access is Denied" error.
Tinkering and experimenting led me to discover the likely problem is that even the Administrator account cannot read this hidden directory
C:\Documents and Settings
. It looks like this is some kind of a link to actual user profiles. I cancd
into the directory, but not list its contents. I can further run thiscd c:\Documents and Settings\Administrator
and see the same contents asC:\Users\Administrator.
The fact that I cannot list the contents of
C:\Documents and Settings
really isn't a problem, since the error should be suppressed by-ErrorAction
. I don't understand why there is a difference in behavior when I try to assign this command to a variable.Anyone else experience this?
-
Two more tidbits.
This happens even when running as the system account. If I run an elevated command prompt and run
psexec.exe -i -s -accepteula powershell.exe
to get a PowerShell session as System, the behavior persists.I have found what appears to be a workaround: Use
-OutVariable foo
at the end of the command. This still lists all of the results ofGet-ChildItem
for me, but at least it did also store it in a variable. -
More information from experimenting.
Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).
Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.
-
That could be Eddie. I'm experimenting and checking behavior with some of these commands.
-
@eddiejennings said in Powershell Get-Childitem behavior with variables:
More information from experimenting.
Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).
Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.
Do you still get the access issues if you're running powershell as admin?
-
@dafyre said in Powershell Get-Childitem behavior with variables:
@eddiejennings said in Powershell Get-Childitem behavior with variables:
More information from experimenting.
Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).
Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.
Do you still get the access issues if you're running powershell as admin?
Yes. Logged in as the local administrator account, as well as running as admin (which is the only option for this account).
-
@eddiejennings I assume you also right clicked and selected to [run as administrator]?
My account is an admin also but if i don't do the above some things don't work. -
This is likely because "Documents and Settings" isnt a real folder, just a junction point since Windows 7/Server 2008. Same reason you usually cant open this folder in Explorer.
These shortcuts only exist for legacy Windows things to function. You wont find this fake folder in Windows 10. -
It probably has something to do with it being a junction point.
cmd /c "dir /al"
will show that its a junction point toC:\Users
-
@jmoore said in Powershell Get-Childitem behavior with variables:
@eddiejennings I assume you also right clicked and selected to [run as administrator]?
My account is an admin also but if i don't do the above some things don't work.I did.
-
Must've been that one server. Seems like it's running fine on another one.