Sergey Kim
Sergey Kim

Reputation: 1

How to find PST files on remote computers using Powershell script

I need some help. I cant’t find what’s wrong with my PowerShell script.

The goal is quite simple. I have to find (.*pst)-files on the users profile on domain computers in the network.
Location to search is “C:\Users\”. List of the PC names where exported to listcomputer.txt.
The trouble is the script run with no errors and no message at all.

$computers = Get-Content c:\temp\listcomputer.txt
$filePath = "C:\Users\" 
foreach($computer in $computers)
    {
    if(Test-Connection -ComputerName $computer -Quiet) 
{
Invoke-Command -ComputerName $computer -ScriptBlock 
{Get-ChildItem -Path $filePath -Recurse -Include '*.pst, *.ost'} }}


First of all I’ve to check connectivity to hosts by Test-Connection cmdlet.

Separately each of the command run successfully. I've tried it.
For example: Test-Connection -ComputerName $computer runs with “true” result and it’s OK.
Also
Invoke-Command -ComputerName $computer -ScriptBlock {Get-ChildItem -Path $filePath -Recurse -Include '*.pst'}
The result is displayed data with information about files were find in folders.
But all together run with no visible result in the PowerShell console
console view result

Regards!

Upvotes: 0

Views: 3078

Answers (1)

postanote
postanote

Reputation: 16076

.pst can be located anywhere, even other drives, or attached storage. You are only looking for C:\.

So maybe this refactor to hit all potential connected drives.:

Get-Content -Path 'c:\temp\listcomputer.txt' | 
ForEach-Object { 
    if(Test-Connection -ComputerName $PSItem -Quiet)
    {
        Invoke-Command -ComputerName $PSItem -ScriptBlock {
            (Get-CimInstance -ClassName Win32_LogicalDisk).DeviceID | 
            ForEach-Object {
                If ($PSItem -eq 'C:')
                {Get-ChildItem -Path "$PSItem\Users" -Recurse -Include '*.pst, *.ost'}
                Else {Get-ChildItem -Path $PSItem -Recurse -Include '*.pst, *.ost'}
            }
        } 
    }
}

Upvotes: 1

Related Questions