none
Get Pst files and move RRS feed

  • Question

  • 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




    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Thursday, January 29, 2015 3:14 PM

Answers

  • 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.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:37 AM
    Moderator

All replies

  • 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:\
         }


    ¯\_(ツ)_/¯



    Thursday, January 29, 2015 3:33 PM
    Moderator
  • 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:\
         }
    


    ¯\_(ツ)_/¯

    but this doesn't know to read the path from txt and move the PST to specific location

    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Friday, January 30, 2015 1:05 PM
  • 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:\
         }


    ¯\_(ツ)_/¯

    but this doesn't know to read the path from txt and move the PST to specific location

    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Yes it does.  Did you try it?  Unfortunately you have to ddisconenct the store and thenreconnect it after the move.  There is a script in the Gallery that does all  of this.  It has been around for some time.


    ¯\_(ツ)_/¯

    Friday, January 30, 2015 5:15 PM
    Moderator
  • Ut is a typo -

    ForEach-Object

    Change the script by changing the = to a -


    ¯\_(ツ)_/¯


    Sunday, February 1, 2015 7:47 AM
    Moderator
  • 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:\
    


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 7:53 AM
    Moderator
  • 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 



    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Sunday, February 1, 2015 7:55 AM
  • 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.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 7:59 AM
    Moderator
  • 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:\}


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 8:01 AM
    Moderator
  • 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.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 8:06 AM
    Moderator
  • 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 1, 2015 8:17 AM
    Sunday, February 1, 2015 8:08 AM
  • 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.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 8:13 AM
    Moderator
  • 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


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Sunday, February 1, 2015 8:19 AM
  • 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:\}



    ¯\_(ツ)_/¯


    Sunday, February 1, 2015 8:27 AM
    Moderator
  • 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 ?


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012


    • Edited by Meir Peleg Sunday, February 1, 2015 8:33 AM
    Sunday, February 1, 2015 8:32 AM
  • You are still not running the correct script.  please copy and run the correct script.  If you cannot do that i cannot help you.


    ¯\_(ツ)_/¯


    Sunday, February 1, 2015 8:33 AM
    Moderator
  • what not correct man?!!

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

    your script dones't work !

    Never mind .


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Sunday, February 1, 2015 8:48 AM
  • The error emssage proves you used an old version.  Get the last version I posted. 

    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 8:55 AM
    Moderator
  • 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:\}


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Sunday, February 1, 2015 8:59 AM
  • 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 }


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:10 AM
    Moderator
  • You can also get fancy with it:

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


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:17 AM
    Moderator
  • 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 ,


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    Sunday, February 1, 2015 9:18 AM
  • 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
    }
    


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:20 AM
    Moderator
  • 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 ,


    My Website:www.Pelegit.co.il Mcitp /Mcsa 2012

    The code works with no issues.  You need to look very closely at the error. If it says the file is open then Outlook is nott closed.  The process amy be exited but the object can hold the store file open until the objects are released.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:22 AM
    Moderator
  • 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.


    ¯\_(ツ)_/¯

    Sunday, February 1, 2015 9:37 AM
    Moderator
  • 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.


    ¯\_(ツ)_/¯

    It can also be Lync holding the file open, if you've got it configured to save your conversation history in your Conversations folder in Outlook.  

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, February 1, 2015 3:48 PM