Skip to content

Check for folders and files where access is denied with Powershell

20/03/2017

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.

Advertisements

From → Powershell

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: