Asked by:
Get-Child Item access is denied

Question
-
Hello,
Trying to find all .PST on a local machine and my script is working but shows access is denied which i believe its trying to search a blocked folder in C:\Windows which i dont want to search for.
I've tried !S on attributes to stop it searching for system files but still no joy any ideas .
if ($true) {Get-ChildItem -path c:\ -Recurse -Include *.PST -ErrorAction SilentlyContinue -Exclude $dontwant -Attributes !S Write-host "Found PST"}
Here is the output
Directory: C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01/08/2018 15:53 0 *@btinternet.com.pst
-a---- 01/08/2018 15:53 0 *@btinternet.com.pst
Get-ChildItem : Access is denied
At line:48 char:13
+ if ($true) {Get-ChildItem -path c:\ -Recurse -Include *.PST -ErrorAct ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedA
ccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
erShell.Commands.GetChildItemCommand
Found PST
- Edited by NathanPEdwards Monday, August 20, 2018 9:55 AM
Monday, August 20, 2018 9:54 AM
All replies
-
Why check the entire C drive? doubt any normal user would move a PST outside of their own home folder.
You could use a try catch statement to catch the error and either disregard them or output the error in a more custom way.
Monday, August 20, 2018 10:03 AM -
Hi NathanPEdwards,
Scanning the whole system drive to find an .PST-file makes no sense when you have propely configure your environment. In the normal way a .PST-file will be stored in the userfolder C:\Users\%username%\AppData\Local\Microsoft\Outlook(IMAP or Exchange Cache) or in the C:\Users\%username%\Documents\Outlook-files(POP3).
See this example for scanning a userfolder:
$ErrorActionPreference = "silentlycontinue" $Extension = "*pst" $TargetFolder = "C:\users\peterpan" Get-Childitem $TargetFolder -Include $Extension -Recurse
If you still want to scan the whole system drive, exclude the system folders:
$ErrorActionPreference = "silentlycontinue" $Extension = "*pst" $TargetFolder = "C:\" $Exclude = @("PerfLogs", "Backup", "boot", "MSOCache", "programdata", "Recovery", "Windows", "Program Files", "Program Files \(x86\)", "System Volume Information" , '\$Recycle\.Bin', "Config\.Msi") try { $error.Clear() Get-Childitem $TargetFolder -Include $Extension -Exclude $Exclude -Recurse } Catch { $ErrorMessage = $_.Exception.Message Write-Host "Error: $ErrorMessage" -f Red break }
I think that you have enough information to go further. If you have any questions do not hesitate to ask them.
Cheers,
Martien van Dijk
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Check my Blog:
https://windowstechblog.nl
Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
- Edited by Martien van Dijk Monday, August 20, 2018 10:43 AM
- Proposed as answer by I.T Delinquent Monday, August 20, 2018 1:09 PM
Monday, August 20, 2018 10:34 AM -
What is $dontwant set to?
Monday, August 20, 2018 12:48 PM -
Many thanks you have pointed me exactly where i needed to go !
I know users shouldn't have saved PST outside their profile but users being users we have to find them as we had a situation a while ago where a machine popped and it had an old archive on the local drive.Monday, August 20, 2018 1:18 PM -
Variable to Stop finding personal email account PSTMonday, August 20, 2018 1:19 PM
-
I meant exactly.
Monday, August 20, 2018 4:29 PM