# Using Invoke-Command to get around UnathorizedAccessException

**URL:** <https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095>\
**Category:** PowerShell Help\
**Created:** [July 30, 2017, 11:57pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095 "2017-07-30T23:57:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [July 30, 2017, 11:57pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/1 "2017-07-30T23:57:36Z")

</div>

I’m trying to understand the use of Invoke-Command in a very specific scenario.

```
Get-ChildItem -Path C:\Windows\csc -Recurse -File
```

Running the above command produces the following error:

```
Get-ChildItem : Access to the path 'C:\Windows\csc' is denied.
At line:1 char:1
+ Get-ChildItem -Path C:\Windows\csc -Recurse -File
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (C:\Windows\csc:String) [Get-C 
   hildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell. 
   Commands.GetChildItemCommand
```

```
Invoke-Command -ComputerName localhost -ScriptBlock {Get-ChildItem -Path C:\Windows\csc -Recurse -File}
```

Wrapping that same command in the ScriptBlock of Invoke-Command as shown above  
will actually output all the files in that folder.

What is Invoke-Command doing that allows it to output all the files instead  
of generating an error?

---

<div class="post-metadata">

**Author:** ![bencamareno](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@bencamareno](https://forums.powershell.org/u/bencamareno)\
**Post date:** [July 31, 2017, 1:58am UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/2 "2017-07-31T01:58:04Z")

</div>

Running from ISE or normal Powershell console?

---

<div class="post-metadata">

**Author:** ![deepitpro](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@deepitpro](https://forums.powershell.org/u/deepitpro)\
**Post date:** [July 31, 2017, 4:46am UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/3 "2017-07-31T04:46:15Z")

</div>

Is PowerShell in administrator mode? check the rights of the user by which you are executing the command.

---

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [July 31, 2017, 6:44am UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/4 "2017-07-31T06:44:51Z")

</div>

I get the same result from the console or the ISE running as administrator. Both commands are run back to back.

Test it out. I imagine you get the same result.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [July 31, 2017, 2:36pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/5 "2017-07-31T14:36:02Z")

</div>

If you try to look at the permissions on the folder you’ll see you don’t have any. Its not a PowerShell issue its a Windows permissions issue. PowerShell won’t and can’t so anything that you don’t have permissions to do or access

---

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [July 31, 2017, 5:38pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/6 "2017-07-31T17:38:47Z")

</div>

I understand I don’t have permissions to that folder which explains the default behavior of Get-Childitem and the access denied error. When wrapped in the scriptblock of Invoke-Command though I can view the files in that folder just fine. So what is Invoke-Command doing that allows me to view the files of a folder that I don’t have permissions to?

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [July 31, 2017, 6:30pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/7 "2017-07-31T18:30:24Z")

</div>

At the below post, the moderator says “The difference is that, with Invoke, you are bypassing UAC.” Not sure if that’s the answer, but there ya go.

[https://social.technet.microsoft.com/Forums/windowsserver/en-US/4d78bba6-084a-4a41-8d54-6dde2408535f/get-folder-where-access-is-denied?forum=winserverpowershell](https://social.technet.microsoft.com/Forums/windowsserver/en-US/4d78bba6-084a-4a41-8d54-6dde2408535f/get-folder-where-access-is-denied?forum=winserverpowershell)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/using-invoke-command-to-get-around-unathorizedaccessexception/9095/8 "2024-05-16T20:38:23Z")

</div>


