-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Rather than either producing a sensible error, or behaving in a reasonably predictable manner, Get-Content incorrectly throws an Access Denied error when pointed at a container object. This is also the case when using the variable accessor syntax.
Steps to reproduce
PS> Get-Content c:\or
PS> ${c:\}Expected behavior
Option #1: A sensible error is produced
Get-Content : Unable to access item contents as it is a container. You may be looking for Get-ChildItem instead.
At line:1 char:1
+ Get-Content c:\
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ***: (C:\:String) [Get-Content], ***
+ FullyQualifiedErrorId : ***,Microsoft.PowerShell.Commands.GetContentCommand
Option #2: Route the call through to Get-ChildItem instead.
What is the "content" of a container if not its child items? It would make at least intuitive sense for Get-Content to simply call Get-ChildItem if passed a path to a container object instead of a leaf object.
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 10/20/2017 2:55 PM msys64
d----- 4/11/2018 7:38 PM PerfLogs
d-r--- 9/11/2018 2:11 PM Program Files
d-r--- 8/27/2018 6:53 AM Program Files (x86)
d----- 10/17/2017 2:52 PM tmp
d-r--- 5/16/2018 3:10 AM Users
d----- 11/6/2017 2:57 PM usr
d----- 9/3/2018 5:02 AM Windows
Actual behavior
(Cmdlet access)
Get-Content : Access to the path 'C:\' is denied.
At line:1 char:1
+ Get-Content c:\
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\:String) [Get-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
(Variable access syntax)
Access to the path 'C:\' is denied.
At line:1 char:1
+ ${C:\}
+ ~~~~~~
+ CategoryInfo : PermissionDenied: (C:\:String) [], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.1.0-rc.1
PSEdition Core
GitCommitId 6.1.0-rc.1
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0In my opinion, given the fact that the variable access syntax is a bit... edge case at the best of times (although undoubtedly also very handy at times) it would seem to make sense that Get-ChildItem would be called in these cases, so that it's possible to explore a provider in this fashion without necessitating use of the cmdlets at all.