# Script not working as a function

**URL:** <https://forums.powershell.org/t/script-not-working-as-a-function/12545>\
**Category:** PowerShell Help\
**Created:** [May 11, 2019, 7:06am UTC](https://forums.powershell.org/t/script-not-working-as-a-function/12545 "2019-05-11T07:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chexmix](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@chexmix](https://forums.powershell.org/u/chexmix)\
**Post date:** [May 11, 2019, 7:06am UTC](https://forums.powershell.org/t/script-not-working-as-a-function/12545/1 "2019-05-11T07:06:51Z")

</div>

This code works just fine but when I put it into a function it returns nothing.

```
$folders = Get-ChildItem -Path c:\users -Directory -Depth 1

$Report = @()
Foreach ($folder in $Folders) {

$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access)
{
$report += [PSCustomObject] @{'FolderName'=$Folder.FullName;
'ADGroup or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited
} 
}
}

$report
```

The same code just put into a function and it returns nothing.

```
function get-folderpermissions{

$folders = Get-ChildItem -Path c:\users -Directory -Depth 1

$Report = @()
Foreach ($folder in $Folders) {

$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access)
{
$report += [PSCustomObject] @{'FolderName'=$Folder.FullName;
'ADGroup or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited
} 
}
}

}

$report
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![tmjr75](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@tmjr75](https://forums.powershell.org/u/tmjr75)\
**Post date:** [May 11, 2019, 7:20am UTC](https://forums.powershell.org/t/script-not-working-as-a-function/12545/2 "2019-05-11T07:20:33Z")

</div>

You need to output the $report within the function.

Something like this:

function get-folderpermissions{

$folders = Get-ChildItem -Path c:\users -Directory -Depth 1

$Report = @()  
Foreach ($folder in $Folders) {

$Acl = Get-Acl -Path $Folder.FullName  
foreach ($Access in $acl.Access)  
{  
$report += [PSCustomObject] @{‘FolderName’=$Folder.FullName;  
‘ADGroup or User’=$Access.IdentityReference;‘Permissions’=$Access.FileSystemRights;‘Inherited’=$Access.IsInherited  
}  
}  
}  
$report  
}

---

<div class="post-metadata">

**Author:** ![chexmix](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@chexmix](https://forums.powershell.org/u/chexmix)\
**Post date:** [May 11, 2019, 7:28am UTC](https://forums.powershell.org/t/script-not-working-as-a-function/12545/3 "2019-05-11T07:28:54Z")

</div>

Dang, that was an easy one.

---

<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:32pm UTC](https://forums.powershell.org/t/script-not-working-as-a-function/12545/4 "2024-05-16T20:32:51Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


