Skip to content

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:

gci -recurse -Path "C:\test" -ea SilentlyContinue -ErrorVariable +errors | Out-Null
$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:


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.

From → Powershell

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: