22

I'm working with PowerShell, running a script (from my console) that includes this line:

$inpath = "C:\users\xxxxx\path\foo\bar"

and I keep getting this error:

Get-Content : Access to the path 'C:\users\xxxxx\path\foo\bar' is denied.
At C:\users\xxxxx\path\foo\testscript.ps1:53 char:12
+ Get-Content <<<<  $txtfile | Get-WordCount -Exclude (Get-Content c:\temp\exclude.txt) | select -First 15
    + CategoryInfo          : PermissionDenied: (C:\users\xxxxx\path\foo\bar:String) [Get-Content], UnauthorizedAcc
   essException
    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

The scripts and target files are all located on my local drive. I can access the files in Explorer, view/edit/save them using NotePad, and do not have any permissions restrictions set. When I'm on the command line, I can run the get-content cmdlet successfully on files in my path. I can change directories PS C:> cd C:\users\xxxxx\path\foo\bar and successfully list what's there. Even more interesting, I can duplicate the line that's erroring in the script, and NOT receive an error on the command line.

PS C:\users\xxxxx\path\foo> $inpath = "C:\users\xxxxx\path\foo\bar"
PS C:\users\xxxxx\path\foo>

This makes me suspect that the 'Permission Denied' error is actually something else, or something vague enough that I've got no clue how to proceed with troubleshooting. Is it possible for PS to have different permissions than the user under which it's running? Has anyone seen this behavior before, and how did you solve the problem? I'm sure there's a simple solution that I don't know.

dwwilson66
  • 6,806
  • 27
  • 72
  • 117
  • How are you running the script and with what permission? A job in Task Scheduler? Does the script work if you call the script from you console? – Frode F. Jan 23 '13 at 16:01
  • What file are you trying to read? (It is perfectly possible to have access to a folder but not the files it contains.) – Richard Jan 23 '13 at 16:01
  • @Graimer - script is running from the console; not sure what permissions the script has, how to look or how to set them if they're different from my user permissions. It does not work from the console AS A SCRIPT (e.g., PS c:\foo> .\script.ps1), but it does work if I type the individual line as the console prompt. – dwwilson66 Jan 23 '13 at 16:09
  • @Richard - eventually, I'm trying to read all files in my $inpath. Right now, I've got a single ASCII file called "testfile.txt". I am unable to access this file from the script, but can read its contents when I manually enter the command at the PS console prompt – dwwilson66 Jan 23 '13 at 16:11

3 Answers3

45
Get-Content : Access to the path 'C:\users\xxxxx\path\foo\bar' is denied.
At C:\users\xxxxx\path\foo\testscript.ps1:53 char:12

That path doesn't look like it is a file but a folder.

Are you sure you are appending the file name to the folder and passing that to Get-Content?

Windows gives Access Denied when you try and open a directory as if it were a file without passing extra flags; and .NET does not pass those flags (there are a few specific circumstances for opening a folder, but they do not apply here).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • You're corrent, `$inpath` is a path. The script is using a `foreach` with the path... `foreach ($doc in $inpath { do-function } )`. I've recycled this code from a prior script that works perfectly... – dwwilson66 – dwwilson66 Jan 23 '13 at 16:50
  • @dwwilson66: I'm nopt talking about a variable: *look at the error message*, the path it shows is the path passed to `Get-Content` and couldn't be opened. – Richard Jan 23 '13 at 17:01
  • I would have loved to know which are those extra flags needed. – DarioP Feb 17 '19 at 16:33
4

Get-Content read contents of file not folder. Please add . after your your folder path like below.

Get-Content "D:\Logs\*.*" | ?{($_|Select-String "test")}

If you want to go through all folders way under it then add -recurse like below:

Get-Content "D:\Logs\*.*" -Recurse | ?{($_|Select-String "test")}
Avijit Chatterjee
  • 1,015
  • 9
  • 9
0

Instead of this: (as per your comment)

foreach ($doc in $inpath) { do-function }

try this:

foreach ($doc in (gci $inpath)) { do-function }

You are doing a foreach on a string object instead of your folder items.

mousio
  • 10,079
  • 4
  • 34
  • 43