0
votes

I would appreciate some help here.

  1. The Powershell script should close Outlook process which works.

  2. Aswell as scan C disk for .pst files which works.

  3. Copy these files to "\fileserver01\temp\test\"

  4. Export to csv/excel list where these files where located and last write time.

  5. Possible hide error messages for the user when running the script since it complains about not full access on a few folders when running the scan.

Code:

Get-Process outlook | Foreach-Object { $_.CloseMainWindow() }

Get-ChildItem -path c:\ -recurse -include *.pst | `
Copy-Item -destination "\\fileserver01\temp\test\" | `
Select-object fullname,lastwritetime|export-csv "\\fileserver01\temp\test\"

How should I fix the last couple of things on my list? Thanks

5
For 1. on your list you can use Stop-Process -Name Outlook* - Jelphy
Don't forget to mark the right answer as such. - Adamar

5 Answers

0
votes

First you have to use double backslash for UNC paths. Second, the copy-item does not output anything to the pipeline, you have to use the -Passthru parameter.

Get-ChildItem -path z:\ -recurse -include *.pst  -PipelineVariable source | 
    Copy-Item -Destination "\\path\temp" -Verbose -PassThru | 
    Select-Object @{n="Source";e={$source.versioninfo.filename}},fullname,lastwritetime | export-csv "\\path\temp\copy_result.csv" -Append -Verbose
0
votes

I believe the issue is that after the files are copied, the object is gone from the pipeline.

This works:

Get-ChildItem -Path C:\ -Include *.pst -ErrorAction SilentlyContinue | Select-Object FullName, LastWriteTime | Export-Csv -Path "\fileserver01\temp\test\MyCSV.csv"
0
votes

This doesn't directly answer the question you've asked, as @Adamar's answer appears to do just that.

however, your issue could also be resolved by querying ost/pst files from registry using a snippet like this:

(Get-ChildItem HKCU:\Software\Microsoft\Office\16.0\Outlook\Search).Property | ? {$_ -match "(o|p)st$"}

which will return all of the ost/pst files the logged in user has open in outlook.

a snippet like this will then copy them all to a network share and print the logs to a file.

$FilesToCopy = (Get-ChildItem HKCU:\Software\Microsoft\Office\16.0\Outlook\Search).Property | ? {$_ -match "(o|p)st$"}
$FilesToCopy | ForEach { Copy-Item -Path $_ -Destination "\\network\share" ; $_ | Out-File "\\network\share\log.txt" -Append }

This saves a LOT of time over indexing through all of the C: drive - there's also an issue where very long directory names (greater than 260 char length) are not indexed properly by Get-ChildItem - making this method a bit more reliable and appealing for a production script.

0
votes

This is the final code. Thanks everyone for your support.

#Kill Oulook process
Get-Process outlook -ErrorAction SilentlyContinue |   Foreach-Object { $_.CloseMainWindow() } 

#Scan for .pst files on the C disk
Get-ChildItem -path c:\ -recurse -include *.pst -ErrorAction SilentlyContinue |

#Copy located .pst files to the destination
Copy-Item -Destination "\\networkpath\home\$env:username\ComputerChange\" -Verbose -PassThru -ErrorAction SilentlyContinue | 

#Log where files were located and when they were last written to.
Select-Object fullname,lastwritetime | export-csv \\networkpath\home\$env:username\ComputerChange\PSTlog.csv -Verbose


Write-Host "PST Files have successfully been copied, press any key to close" -ErrorAction SilentlyContinue
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
end
0
votes

So I have created a much faster script as I have excluded some systemfolders and programfiles folders where .PST files doesn't save. I bet some of you expert can find out why this code doesn't work?

#Exclude systemfolders etc
$folders=get-childitem c:\ | where-object{$_.mode -like "d-*" -AND $_.name -notlike "windows" -AND $_.name -notlike "drivers" -AND $_.name -notlike "program files*"}

#Search thru each root folder for PST-files
$allfiles=$folders | ForEach-Object{get-childitem -Path $_.fullname -include "*.pst" -recurse -ErrorAction silentlycontinue};

$env:username
$foldertocreate="\\destination\$env:username\"

#Check if folder with username exists in the \\destination folder otherwise create folder with username.       
if((Test-Path -Path $foldertocreate -PathType Container)) {write-host "Folder already created"}

        else {write-host "Creating Folder", New-Item -ItemType Directory -Force -Path $foldertocreate }                              

#Copy .PST files which is in $allfiles to the folder created in fileshare> $foldertocreate.

#Copy .PST files in $allfiles to the destination folder created.
robocopy $allfiles $foldertocreate

Write-Host "Press any key to close" -ErrorAction SilentlyContinue $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
end