Check for folders and files where access is denied with Powershell

The following script is from another blog that was slightly adapted by someone who left a comment there:

$errors=@()
gci -recurse -Path "C:\test" -ea SilentlyContinue -ErrorVariable +errors | Out-Null
$errors.Count
$errors | select -expand categoryinfo | select reason,targetname | export-csv -NoTypeInformation -Delimiter “;” ErrorList.csv

I created a folder on the root of my C: drive called test, I then removed permission granting full rights only to a specific account so I could then later remove the folder. The CSV file will be written to the current directory so you need to ensure that it is possible to write there.
-ea SilentlyContinue (-ea is an abbreviation for -ErrorAction) tells the script to carry on with no output if it hits an error, the error is then added to the errors variable ($errors). Further information can be found on this MSDN blog. Out-Null tells the script not to output anything to the Window.
Next the number of errors encountered are displayed and the file with it’s accompanying error are written out to the specified CSV file.
After executing the script, the CSV file contents look like this:

Reason;”TargetName”
UnauthorizedAccessException;”C:\test”

It may be useful to combine the output from this script with the script to remove the read only file attribute in a previous post so that any exceptions are captured and can be reviewed.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *