Get Pst files and move

Hi , all

difficult situation below :

i have another script that get me all pst name + Their path  that connected to my outlook :

$usr=[Environment]::UserName $dmn=[Environment]::UserDomainName $comp=[Environment]::MachineName $outlook = New-Object -comObject Outlook.Application $final=$outlook.Session.Stores | where { ($_.FilePath -like '*.pst')} | select FilePath$final >> c:\3.txt$new = import-csv c:\3.txtdel c:\3.txtforeach($line in $new){$line = $line.trim()'"'+$line+'"'>>c:\3.txt}

Result :

"FilePath"
"C:\Users\mtest\Desktop\My Outlook Data File(1).pst"
"C:\Users\mtest\Documents\My Outlook Data File(2).pst"
"C:\Users\mtest\Pictures\My Outlook Data File(3).pst"
"C:\Owo.pst"

as you see all pst files exists in different location , i would like to special script that know read the paths of pst and move them to specific location like X: or whatever


I have a script that "move " all my pst files to new location , see below the script :

Get-ChildItem -Path "C:\Users\mtest\Documents\Outlook Files\" -Filter *.pst -Recurse | Move-Item -Destination "x:\"

my problem is that my script knows to move them from specific location to specific destination , maybe there are another options that can read several path and perform move ? 

Thank you very much for the effort



January 29th, 2015 6:14pm

This gets all paths.

$usr=[Environment]::UserName
$dmn=[Environment]::UserDomainName
$comp=[Environment]::MachineName
$outlook= ew-Object -comObject Outlook.Application
$final=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach-Object{
	Move-Item $_.FilePath X:\
     }

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 6:33pm

This gets all paths.

$usr=[Environment]::UserName
$dmn=[Environment]::UserDomainName
$comp=[Environment]::MachineName
$outlook=New-Object -comObject Outlook.Application
$final=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach-Object{
	Move-Item $_.FilePath X:\
     }

January 29th, 2015 6:33pm

This gets all paths.

$usr=[Environment]::UserName
$dmn=[Environment]::UserDomainName
$comp=[Environment]::MachineName
$outlook= ew-Object -comObject Outlook.Application
$final=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach=Object{
	Move-Item $_.FilePath X:\
     }

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 4:05pm

This gets all paths.

$usr=[Environment]::UserName
$dmn=[Environment]::UserDomainName
$comp=[Environment]::MachineName
$outlook= ew-Object -comObject Outlook.Application
$final=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach=Object{
	Move-Item $_.FilePath X:\
     }

January 30th, 2015 8:15pm

Ut is a typo -

ForEach-Object

Change the script by changing the = to a -

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 2:49am

Do it this way to avoid issues:

$outlook= New-Object -comObject Outlook.Application
$files=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach-Object{$_.FilePath}
$outlook.Quit()
Move-Item $files X:\

February 1st, 2015 2:56am

Hi ,

Sorry for the lack of attention - Lol ...

 tried again to run the script and again encontered in problem , 

when i run the script -so the script open the outlook ( behind the scene ) and then i got an error on the powershell " The process cannt access the file cecuase it is being used by another process "

is not possbile move or access to file when the outlook is open ,

I even tried to close the outlook and then run again 


Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 2:58am

As I posted above. Your code won't work at all because you have to quit Outlook before you can move the files. THat is why I posted the new version.

February 1st, 2015 3:00am

Note that any program including PowerSHell can lock a COM object inmemmory causing the file to remain open and locked.  Thisincludes other Offic products that may be optioed to integrate with OOutlook, other applications that access Outlook and AV software that scans PST files and some that are optioned to protect PSTs from being accesssed by trojans.

Trojans and viruses can also cause theOCM object to remain in memory keeping the file locked. 

It is up to you to track down the casue.  The code wotks on all versions of Offise from 2007 on and n W7 and W8/8.1.  It mamy not wrk on Windows XP or on Ofice 2003 and earlier.

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 10:52am

You can also try it like this:

$outlook= New-Object -comObject Outlook.Application
$files=$outlook.Session.Stores | 
     where {$_.FilePath -like '*.pst'} |
     ForEach-Object{$_.FilePath}

$outlook.Quit()

$files | %{Move-Item $_ X:\}

February 1st, 2015 11:01am

You also need to know that, if you move the PSTs they will no longer work with Outlook and Outlook will complain about mmissing files.

To move an outlook datafile you must disconnect it, move  it thenreconenct it.  You will lose any customization such as search folders and favorite.

Manipulating Outlook programatically is not easy and requires considerable programming experience with Office products.   Companies mamke tools that can move outlook files. Yu might want to purchase one of those.

If you need this to move files for many users you will have to spendconsiderable time testing your script before using it.

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

helli i have quit from outlook before running the script ... its cleary that cannt move the pst when oulook is open,

regarding secound script  - again  same error (

down that outlook is closed ) 



  • Edited by Meir Peleg Sunday, February 01, 2015 8:17 AM
February 1st, 2015 11:08am

You are still not running the correct script.  I told you that one wouldn't work.  I was just showing yu how to use PowerShell.  I thenposted oone that shows how to quit Outllok before moving the files.

I can give you help with PowerShell but you have to do thethinking about what you are doing.

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

Listen i have another script that know to connect the pst file from specific location to outlook ,
you let me close to 3 different script and nothing not working my friend ,
is confused on upload of image and did update please see above

February 1st, 2015 11:19am

This is the only script that can work but you probably have to restart your PC to make it work.  You likely have copies of Outlook stuck in memory.

$outlook= New-Object -comObject Outlook.Application $files=$outlook.Session.Stores | where {$_.FilePath -like '*.pst'} | ForEach-Object{$_.FilePath} $outlook.Quit()

sleep -seconds 30 $files | %{Move-Item $_ z:\}

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

I did restart and then run script , again same error :

PS X:\> C:\Users\mtest\Desktop\test3.ps1
Move-Item : The process cannot access the file because it is being used by another process.
At C:\Users\mtest\Desktop\test3.ps1:8 char:21
+ $files | %{Move-Item <<<<  $_ x:\}
    + CategoryInfo          : WriteError: (C:\Users\mtest\...ata File(1).pst:FileInfo) [Move-Item], IOException
    + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

Again i told you in proccess of script tour script open the outlook and try to move becuase it the script dones't work 

maybe before you write me try the script yourself test outlook ?

February 1st, 2015 11:32am

You are still not running the correct script.  please copy and run the correct script.  If you cannot do that i cannot help you.

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

what not correct man?!!

i runned what you let me , you did edit to your post anytime 

your script dones't work !

Never mind .

February 1st, 2015 11:48am

The error emssage proves you used an old version.  Get the last version I posted. 
Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 11:55am

this is new version right ?:

$outlook= New-Object -comObject Outlook.Application $files=$outlook.Session.Stores | where {$_.FilePath -like '*.pst'} | ForEach-Object{$_.FilePath} $outlook.Quit()

sleep -seconds 30 $files | %{Move-Item $_ z:\}

February 1st, 2015 11:59am

If that doesn't work it means that Outlook is not shutting down. You have an issue with Outlook.

From the error:

The process cannot access the file because it is being used by another process.

YOU have to be sure to release Outlook and that it will shutdown.  This is a known issue with Outlook.

YOu can add code to release the COM object and check to be sure the process has quit;

While(Get-Process outlook){ sleep 5 }

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 12:10pm

You can also get fancy with it:

$outlook.Quit()
while(Get-Process Outlook -ea 0){
    Write-Host 'Sleeping 5' -Fore green
    sleep 5
}

February 1st, 2015 12:17pm

I checked and sure 100 % that Outlook isn't open ,
i even did check on Task Manager to verify that outlook really closed
properly your script doesn't work correct .
finally -
thank you about your efforts ,

Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 12:18pm

For some versions of Outlook you need to do this:

$outlook.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook)){}
while(Get-Process Outlook -ea 0){
    Write-Host 'Sleeping 5' -Fore green
    sleep 5
}

February 1st, 2015 12:20pm

I checked and sure 100 % that Outlook isn't open ,
i even did check on Task Manager to verify that outlook really closed
properly your script doesn't work correct .
finally -
thank you about your efforts
Free Windows Admin Tool Kit Click here and download it now
February 1st, 2015 12:22pm

Note that any program including PowerSHell can lock a COM object inmemmory causing the file to remain open and locked.  Thisincludes other Offic products that may be optioed to integrate with OOutlook, other applications that access Outlook and AV software that scans PST files and some that are optioned to protect PSTs from being accesssed by trojans.

Trojans and viruses can also cause theOCM object to remain in memory keeping the file locked. 

It is up to you to track down the casue.  The code wotks on all versions of Offise from 2007 on and n W7 and W8/8.1.  It mamy not wrk on Windows XP or on Ofice 2003 and earlier.

February 1st, 2015 12:37pm

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

Other recent topics Other recent topics