Avatar of Software Squirrel
Software SquirrelFlag for United States of America

asked on 

Powershell to loop through a file system and report permissions on each file

I need a PowerShell script to loop through a file system and display the file name and permissions for that file.  Preferably into an excel file.
PowershellWindows 10AzureWindows Server 2016

Avatar of undefined
Last Comment
Dorababu M
Avatar of Dorababu M
Dorababu M
Flag of India image

You can do as below

$AllFolders = Get-ChildItem -Directory -Path "\\Server\StaffShared$\" -Recurse -Force
$Results = @()
Foreach ($Folder in $AllFolders) {
    $Acl = Get-Acl -Path $Folder.FullName
    foreach ($Access in $acl.Access) {
        if ($Access.IdentityReference -notlike "BUILTIN\Administrators" -and $Access.IdentityReference -notlike "domain\Domain Admins" -and $Access.IdentityReference -notlike "CREATOR OWNER" -and $access.IdentityReference -notlike "NT AUTHORITY\SYSTEM") {
            $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
            $Results += New-Object -TypeName PSObject -Property $Properties
        }
    }
}

$Results | Export-Csv -path "C:\Permissions - $(Get-Date -format MMyy).csv"

Open in new window


https://community.spiceworks.com/topic/1162170-get-folder-permissions-export-to-file-and-sort
Avatar of Software Squirrel

ASKER

I get a couple
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply an argument that is not n
ull or empty and then try the command again.
Unable to find type [ordered]: make sure that the assembly containing this type is loaded.
Avatar of Dorababu M
Dorababu M
Flag of India image

Remove -Directory and try once
Avatar of Software Squirrel

ASKER

When I do that, I get this error over and over:

New-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Supply an argument that i
s not null or empty and then try the command again.
At C:\PS1\GetPermissions.ps1:8 char:64
+             $Results += New-Object -TypeName PSObject -Property <<<<  $Properties
    + CategoryInfo          : InvalidData: (:) [New-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewObjectCommand
Avatar of Dorababu M
Dorababu M
Flag of India image

I am not sure but it works for me may be just check
User generated image
if(!$Properties)
{
     $Results += New-Object -TypeName PSObject -Property $Properties
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dorababu M
Dorababu M
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo