Searching folder

Hi,

I have this script to search drives for PST files

Get-PSDrive -PSProvider "filesystem"|%{get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}}

I want it to tell me what folder it is searching and highlight whenever it come across a folder it doesn't have access to.

I get the error message "get-childitem <folder> is denied and the full reason but I want it to look neater and just display something like "Access denied to <folder>" or "Searching <folder>"

TIA

Andy

February 10th, 2015 4:27pm

For searching PST files you can use this query as well

gwmi -Query "Select * from CIM_DataFile Where Extension = 'pst'"

February 11th, 2015 9:10am

I have looked at that but cant get it to do what I want.

I want it to tel me which directory it is searching and which ones it doesn't have access to

Try{

get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}

}

Catch [systemexception]{

write-host $_." - Access is denied" -f yellow

}

It still gave me the same results

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 11:11am

PowerShell CmdLets cannot do that unless they are designed to do that.

What you are asking doesn't make sense because you have no access to something so you cannot know what it is until you try to access it.

You can do this as a consolation prize"

get-childitem c:\windows\* -include *.pst -rec -ea 0 -ev myerrors|
    select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}}

$myerrors | select TargetObject

February 11th, 2015 11:25am

Thanks, that does work but doesn't give me what I was looking for.

I want to search my network for PST files.

I would like the script to say which directory it is searching. I can get it to do which drive but not which folder/directory

If it finds one it doesn't have permissions to I would like it to give a message back rather than the exception/error message you normally get.

I am just trying to make things neater.

The script example only shows c:\ but in practice it is searching network drives as well so that is why I want to know where the script is searching.

 Get-PSDrive -PSProvider "filesystem"|%{get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}}

TIA

Andy

Free Windows Admin Tool Kit Click here and download it now
February 11th, 2015 11:48am

Sorry.  You cannot do that with PowerShell without extra bits.

Why is that important?  It sounds like you jsut want something that looks cool but serves no purpose.

The closest you can come to first list all folders then enumerate teh folder list and search for files.  This will be very slow.

$dirs=Get-ChildItem c:\ -directory -recurse

$dirs |
   ForEach-Object{
      "searching $_"
       Get-ChildItem *.pst
   }

February 11th, 2015 11:57am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics